Feeds

Don't unit test GUIs

A different viewpoint

Providing a secure and efficient Helpdesk

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.

Choosing a cloud hosting partner with confidence

More from The Register

next story
Microsoft on the Threshold of a new name for Windows next week
Rebranded OS reportedly set to be flung open by Redmond
Business is back, baby! Hasta la VISTA, Win 8... Oh, yeah, Windows 9
Forget touchscreen millennials, Microsoft goes for mouse crowd
SMASH the Bash bug! Apple and Red Hat scramble for patch batches
'Applying multiple security updates is extremely difficult'
Apple: SO sorry for the iOS 8.0.1 UPDATE BUNGLE HORROR
Apple kills 'upgrade'. Hey, Microsoft. You sure you want to be like these guys?
ARM gives Internet of Things a piece of its mind – the Cortex-M7
32-bit core packs some DSP for VIP IoT CPU LOL
Lotus Notes inventor Ozzie invents app to talk to people on your phone
Imagine that. Startup floats with voice collab app for Win iPhone
'Google is NOT the gatekeeper to the web, as some claim'
Plus: 'Pretty sure iOS 8.0.2 will just turn the iPhone into a fax machine'
prev story

Whitepapers

A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.
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.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.