Overview

Chapter 8. Test-driving Swing

Working with Swing changes the usual TDD conversation because a graphical user interface is judged not only by behavior, but also by usability and appearance. The chapter frames Swing testing around three broad concerns: internal plumbing and utility logic, rendering and layout, and interaction. The first category is treated much like ordinary Java code and should be test-driven normally; the second is usually better verified visually rather than through brittle pixel-by-pixel assertions; and the third, which covers user gestures and widget-to-widget coordination, is the most important behavioral area to automate.

To make Swing code easier to test, the chapter emphasizes keeping the view layer thin by pushing logic out of widgets and into presenter-like structures. It presents classic Model-View-Presenter and two closely related variations, Supervising Controller and Passive View, as practical ways to separate responsibilities. In these designs, the presenter handles user input and model updates, while the view is reduced to either displaying model state or merely relaying gestures and displaying instructions, which makes both sides much easier to unit test.

The chapter then turns to the tools needed when testing actual Swing components cannot be avoided. It compares Jemmy and Abbot as ways to find widgets in a component hierarchy, simulate keyboard and mouse actions, and verify what the UI displays without exposing internals through public accessors. Finally, it demonstrates test-driving a small plot-map component step by step: adding controls, drawing custom graphics, and reacting to mouse clicks that remove plotted points. The example shows that even visually rich Swing code can be developed incrementally and tested effectively when the problem is broken into small, focused behaviors.

The MVC triad and its relationships. The controller acts on the model based on input from the view, the model worries about domain logic unaware of any details of the view or the controller, and the view registers with the model for change notifications and renders the data it obtains from the model.
Diagram showing the interactions between the Supervising Controller MVP-triplet for a green bar user interface
Listing 8.1. The presenter implemented in a Supervising Controller style
Passive View (or The Humble Dialog Box) makes the view as thin as possible, encapsulating as much view logic as possible into the presenter.
Listing 8.2. The presenter implemented in a Passive View style
Listing 8.3. How to verify that the view works?
Listing 8.4. View tests implemented with Jemmy
Listing 8.5. Custom name-based ComponentChooser implementation for Jemmy
Listing 8.6. View tests implemented with Abbot
UI design sketch for the plot map component explaining the desired responses to user gestures
Listing 8.8. Plumbing for a test operating the view implementation
Listing 8.11. View implementation for passing our first test
Listing 8.12. Testing for the view delegating drawing to a PlotMapCanvas
Listing 8.14. Making view pass model to a separate canvas for drawing
Listing 8.15. Explicitly specifying that a view should create exactly one canvas
Listing 8.18. Comparing size and bounds
Listing 8.21. Utility class for capturing a bitmap of a Swing component
Listing 8.22. Test for the canvas drawing the right stuff
Listing 8.23. Rudimentary implementation of drawing custom graphics
Listing 8.24. Test for plot points getting connected
Listing 8.25. Drawing lines between plots
Listing 8.26. Test handling of user gestures
Listing 8.28. Testing for only certain mouse clicks triggering remove events
Listing 8.29. Comparing the source of the mouse click before triggering events

FAQ

What should be tested in a Swing UI?In a Swing UI, you should primarily test internal plumbing and utility logic, interaction behavior, and other code that can be verified without relying on visual inspection. Rendering and layout are usually better checked visually rather than with brittle automated tests.
Why is testing Swing code harder than testing backend code?Backend code usually has a technical interface that is easy to automate. Swing code involves a graphical user interface, where behavior, appearance, and usability all matter. That makes some aspects much harder to verify automatically.
Why does the chapter recommend not testing exact pixel layout?Pixel-precise layout tests are usually too fragile and slow to maintain. Small visual changes can break many tests without improving quality much. The book suggests relying on visual inspection for layout in most cases.
When does rough layout testing make sense?Rough layout tests make sense when you want to verify relative positioning or sizing, such as one button appearing above another or two buttons having the same width. These tests are more robust than pixel-precise ones.
What is the main advantage of the Model-View-Presenter style for Swing?MVP moves most logic out of the view and into a presenter, making the view thinner and easier to test. The presenter can usually be tested as plain Java code without depending on Swing components.
How does Passive View differ from Supervising Controller?In Passive View, the presenter handles both user gestures and view updates, while the view only forwards input and displays data. In Supervising Controller, some view-model synchronization can happen through data binding, so the view may contain a bit more logic.
Why are Jemmy and Abbot useful for Swing testing?They help testers access and manipulate Swing components inside a component hierarchy. They can find widgets, simulate clicks and keystrokes, and inspect component state without exposing internal widgets through public fields or accessors.
What is the difference between Jemmy and Abbot?Jemmy works mainly through the Swing event queue and component search mechanisms, while Abbot uses more operating-system-level events and offers explicit support for unit testing, including helpful test fixtures and utilities.
How can a custom Swing component be tested for drawing output?A common approach is to render the component to an in-memory buffer, extract the bitmap raster, and assert the colors of specific pixels. This works well when the graphical output is small and well-defined.
How did the chapter test mouse clicks on the plot map canvas?It simulated mouse clicks using the component’s event dispatching mechanism and registered a test double as a point-event listener. The tests verified that clicks on plotted points triggered removal events, while clicks elsewhere did not.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free