Feeds

Don't unit test GUIs

A different viewpoint

5 things you didn’t know about cloud backup

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.

Secure remote control for conventional and virtual desktops

More from The Register

next story
BBC: We're going to slip CODING into kids' TV
Pureed-carrot-in-ice cream C++ surprise
China: You, Microsoft. Office-Windows 'compatibility'. You have 20 days to explain
Told to cough up more details as antitrust probe goes deeper
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
Windows 7 settles as Windows XP use finally starts to slip … a bit
And at the back of the field, Windows 8.1 is sprinting away from Windows 8
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

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.
Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?