Feeds

Don't unit test GUIs

A different viewpoint

New hybrid storage solutions

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.

Reducing the cost and complexity of web vulnerability management

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
Not appy with your Chromebook? Well now it can run Android apps
Google offers beta of tricky OS-inside-OS tech
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
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?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.