Feeds

A simple unit test for GUIs

Part 2: Server envy

High performance access to file storage

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.

High performance access to file storage

More from The Register

next story
Android engineer: We DIDN'T copy Apple OR follow Samsung's orders
Veep testifies for Samsung during Apple patent trial
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Windows XP still has 27 per cent market share on its deathbed
Windows 7 making some gains on XP Death Day
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
prev story

Whitepapers

Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
HP ArcSight ESM solution helps Finansbank
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.