Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 550
Allain Lalonde Avatar asked Oct 16 '09 13:10

Allain Lalonde


4 Answers

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.

like image 121
Pascal Thivent Avatar answered Nov 16 '22 04:11

Pascal Thivent


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.

like image 44
Aaron Digulla Avatar answered Nov 16 '22 03:11

Aaron Digulla


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

like image 45
Russ Hayward Avatar answered Nov 16 '22 02:11

Russ Hayward


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/

like image 2
Glen Avatar answered Nov 16 '22 02:11

Glen