Feeds

Don't unit test GUIs

A different viewpoint

Maximizing your infrastructure through virtualization

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 security risks from open source software

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Do YOU work at Microsoft? Um. Are you SURE about that?
Nokia and marketing types first to get the bullet, says report
Microsoft takes on Chromebook with low-cost Windows laptops
Redmond's chief salesman: We're taking 'hard' decisions
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
Big Blue Apple: IBM to sell iPads, iPhones to enterprises
iOS/2 gear loaded with apps for big biz ... uh oh BlackBerry
prev story

Whitepapers

Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
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.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Consolidation: the foundation for IT and business transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.