Easiest way to unit test SWT and Swing apps in a headless environment?
Asked Answered
T

6

20

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).

Timothee answered 16/10, 2009 at 13:18 Comment(0)
L
26

You could run Xvfb (X virtual framebuffer, an X11 server that performs all graphical operations in memory) and this works fine.

But there is another solution with Hudson's plugin for Xvnc. Simply install the plugin and check the checkbox in the job configuration screen:

alt text http://www.justinedelson.com/blog/wp-content/uploads/xvnc_box.jpg

Hudson will then automatically start up a Xvnc session and set the DISPLAY environment variable to the appropriate value and then shut down the session when the build is complete. One advantage this has over the Xvfb method is that if you have multiple Swing/SWT projects building simultaneously, each build has its own X session. This may not be an issue at all but it seems like a good idea.

Before using this plugin, you obviously have to have Xvnc installed. What's less obvious (although sensible) is that you must also set a password. You do this by running:

$ vncpassword

This has to be done as the same user Hudson runs as.

Laband answered 16/10, 2009 at 13:26 Comment(0)
B
3

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.

Brumal answered 16/10, 2009 at 13:26 Comment(0)
R
3

I was sure I posted this here before, not sure what happened to it.

Cacio allows for running Swing app headless.

http://rkennke.wordpress.com/2011/10/17/cacio-for-ui-testing/

Rhetorical answered 4/4, 2012 at 20:41 Comment(1)
This project is now hosted on GitHub github.com/CaciocavalloSilano/caciocavalloRhetorical
T
2

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

Trifacial answered 16/10, 2009 at 16:36 Comment(0)
L
1

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.

Luz answered 16/10, 2009 at 13:25 Comment(0)
R
1

You could try RedDeer Testing framework https://github.com/jboss-reddeer/reddeer

• Support for running tests in a Jenkins CI environment (Hudson like)

Rhyton answered 17/8, 2016 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.