Unit Testing Spring-Based Swing Apps

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

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.