Feeds

A simple unit test for GUIs

Part 2: Server envy

Intelligent flash storage arrays

Hands on Last time I described why GUI code is difficult to unit test, and why it's generally better to avoid doing so. But that doesn't mean GUI-related code shouldn't be tested - you just need to separate out the logic.

Easier said than done? The number of times I've spoken to Java GUI coders who've said: "Swing code can't be unit-tested, so we don't bother." Then they stare wistfully across the room at the server-side developers, whose code is well covered by tests.

So, how do you write unit tests for GUI applications? I'm not claiming that the approach described here is new, but it's simple, and I've found it to be highly effective as it promotes separation of concerns, putting the behaviour where the data is, and other good OO stuff.

First, let's assume you're creating a Swing-based Customer Details panel. So you'll want a CustomerDetailsPanel class that contains the GUI code: components, layout, and Swing event handlers. Next, create a Controller class, CustomerDetailsController, which will contain the business and application logic, and contain references to the entity objects (Beans, POJOs). Any time the GUI needs to check or update something related to the data, it makes a call to its Controller to do it.

The Panel and its Controller are tightly symbiotic: the Panel must know about the Controller in order to delegate to it, and the Controller must know about the Panel to tell it to update the GUI. Design puritans might start hyperventilating at this tightly coupled two-way relationship, but in practice it isn't a problem.

However, it could become a problem when you decide to write some unit tests for the Controller - which of course is the whole point of this exercise. If the Controller contains method calls to the Panel, then you'll also need to create an instance of the Panel in your unit test - something we're actively trying to avoid doing.

The easy solution is to define a UI interface (CustomerDetailsUI), implemented by the Panel and passed into the Controller on creation, which defines all the methods the Controller will want to call on the Panel. Then in your unit test, you can simply define a mostly-empty implementation of the UI interface and pass that into the Controller instance created for testing.

In the example I'm calling this a mock UI, although the Mock Object Correctness Police will doubtless rap my knuckles as it's really a stub implementation, not a mock.

Providing a secure and efficient Helpdesk

More from The Register

next story
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Redmond top man Satya Nadella: 'Microsoft LOVES Linux'
Open-source 'love' fairly runneth over at cloud event
Google+ goes TITSUP. But WHO knew? How long? Anyone ... Hello ...
Wobbly Gmail, Contacts, Calendar on the other hand ...
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
Torvalds CONFESSES: 'I'm pretty good at alienating devs'
Admits to 'a metric ****load' of mistakes during work with Linux collaborators
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.