Unit and integration testing with Spring is fairly straightforward. You can even extend one of the convenient Spring test classes if you need to get multiple beans to interact during the course of a test. Abbot, on the other hand, is an excellent framework for testing Swing GUIs, providing its own convenient base test class, ComponentTestFixture. The trouble with mixing these two frameworks is that you can only extend from one of the available base test classes. After trying a couple of different approaches to unit testing a Spring-based Swing application, the following solution seems to work fairly well:
public class MyTest extends ComponentTestFixture {
private ClassPathXmlApplicationContext applicationContext;
@Override
public void runBare() throws Throwable {
try {
this.applicationContext = this.createContext();
super.runBare();
} finally {
if (this.applicationContext != null) {
this.applicationContext.close();
}
}
}
private ClassPathXmlApplicationContext createContext() {
String[] s = new String[] { /* my config files */ };
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(s);
context.registerShutdownHook();
return context;
}
@Override
protected void fixtureSetUp() throws Throwable {
super.fixtureSetUp();
// Custom initialization.
}
// Test methods.
}
Advertisement