Feeds

Don't unit test GUIs

A different viewpoint

Build a business case: developing custom apps

Part I It can be dangerous to make sweeping "Don't" statements. In Patterns of Enterprise Application Architecture, agile guru Martin Fowler coined his now-infamous first law of distributed object design: Don't distribute your objects. This is good advice to an extent but is also an overly simplistic viewpoint. The reader is left to track down the exceptions to the rule.

With that in mind, I'm going to propose (and then shoot down) an overly simplistic law of unit testing GUIs: Don't unit-test GUIs. It's more trouble than it's worth, and you end up grappling with object lifecycle issues, heavyweight/time-consuming unit tests that create GUI components or stop and wait for user input in a "headless" build environment, and so on. Whole open-source projects have sprung up to try and solve these difficulties: and this in itself is an indictment of the futility of automated GUI testing.

And now to shoot down that same simplistic law. Of course GUI applications can (and should) be unit tested: but it isn't the GUI code itself that you want to test, it's the business and application logic behind the GUI. It's too easy to just say "don't unit test GUIs" because it's difficult. Instead, a simple separation of logic from GUI code means that the stuff which should be tested becomes easy to test; and the mechanical stuff (Does the UI freeze when I click this button? Is the layout correct?) can be left to visual inspection. (You do test your app by running it from time to time, don't you? And not just by waiting for a green bar?) If the UI itself doesn't behave, you'll quickly know about it. But if the application logic is wrong, you might not find out until the application has been released – unless you've got a decent set of unit tests.

Web application frameworks tend to promote MVC-style separation of view from logic, as web apps are inherently multi-layered. Web developers just don't know how good they've got it! However, in the rich client/GUI world, it's trickier. GUI toolkits such as Swing don't do a good job of promoting the separation of GUI code (e.g. onClick event handlers) from logic code. So it's common to see big, monolithic Swing-based classes – dialogs, panels, forms – that perform business logic right there in the Swing event listeners. A good example of this is validation code, like the following:

public void actionPerformed(ActionEvent event) {
String userID = userIdField.getText();
if ("".equals(userID) || userID.length() > 10) {
invalidField("userID",
"User ID must be between 1 and 10 characters.");
}
}

Putting data validation in the form itself violates a golden rule of OO design: always put the behaviour where the data is. In this case, validation should be in the entity/POJO/bean class, not in the GUI code: e.g. have the userID property throw a PropertyVetoException if the new value fails validation.

In Part II of this little series-ette, I'll put my money where my mouth is and outline a useful pattern for separating logic from the Swing GUI. This pattern also gives you the option of "remoting" the controller classes (e.g. turn them into web services), should the inclination grab you. This will allow you to break both Fowler's law of distributed objects and my own law of unit testing GUIs. Go rebel! ®

Matt Stephens co-authored Use Case Driven Object Modeling with UML: Theory and Practice which explores in detail how to drive functional tests and unit tests from use cases.

Gartner critical capabilities for enterprise endpoint backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Top 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Backing up distributed data
Eliminating the redundant use of bandwidth and storage capacity and application consolidation in the modern data center.
The essential guide to IT transformation
ServiceNow discusses three IT transformations that can help CIOs automate IT services to transform IT and the enterprise
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.