views:

345

answers:

4

I'm looking to unit test some SWT and Swing code for a project I'm working on and the tests run fine as long as I'm running them from eclipse.

As soon as I run them in my hudson environment it fails since hudson runs the tests in headless mode.

What's the best way of doing this? Open source solutions only please (since the project is open source).

A: 

I don't know about SWT, but with Swing you can't. Any instantiation of a Window (JFrame, JDialog, etc.) even if it is never set to visible will blow up in headless mode (on JDK 5). What we did was not run in headless mode and install Xvfb to provide the windowing without actually having a real windowing system installed.

Yishai
+1  A: 

Try the Abbot Java GUI Testing Framework and SWTbot. At least SWTbot should be able to do it.

If neither offers a headless mode, then this blog post might give you some ideas how to get rid of the UI for testing.

Aaron Digulla
+3  A: 
Pascal Thivent
If this works, this is the neatest trick I've seen in years!
Allain Lalonde
+1  A: 

Using Swing I tend to organise things so that the component tree can be created without a Window at the top. Doing this allows you to simply create a JPanel in a unit test and use that as your top-level component. There are certain things you cannot test, such as focus and any logic involved in the creation of the Frame for normal operation, but the vast majority can be tested.

You may want to look into the FEST library to make life easier whether you go headless or not, it looks very good: http://fest.easytesting.org/swing/wiki/pmwiki.php

Russ Hayward