The Source for Java Technology Collaboration
User: Password:



Simon Brown

Simon Brown's Blog

Global setUp() and tearDown() in JUnit tests

Posted by simongbrown on April 16, 2004 at 02:43 PM | Comments (5)

Like many people, I want a way to run some one-time set up and tear down logic and the approach I usually take is to drop some code into a static initializer block in an abstract test case. For example...

public abstract class SomeTestCase extends TestCase {

  static {
    // perform the "global" set up logic
  }

}

Providing that I remember to subclass SomeTestCase then this approach works well. So that's set up covered, but what about tear down? Simple - just use a JVM shutdown hook.

public abstract class SomeTestCase extends TestCase {

  static {
    // perform the "global" set up logic

    // and now register the shutdown hook for tear down logic
    Runtime.getRuntime().addShutdownHook(new SomeTestCaseShutdownHook());
  }

}

This is quite a simple solution and is an alternative to the TestSetup class. As an example, I use it for creating a file structure on disk prior to running some tests and deleting that structure when the tests have run. The only caveat with this solution is that when you run your unit tests through Ant, the Ant ClassLoader executes the static initializer (and resets static variables) more than once. I don't know quite why it does this but if your global set up and tear down logic can safely be run more than once then it's not too much of a problem. Incidentally, this technique runs really well with the IntelliJ JUnit runner.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • File.deleteOnExit?
    If it's just files/directories, what's wrong with File.deleteOnExit?

    Ant will need to reload the classes between runs and even different task instances as they may have changed. Also there's not much point attempting to manage the class loaders.

    I couldn't resist hacking some code together that attempts to do the clear up soon after the test have finished running.

    http://jroller.com/page/tackline/20040416

    Posted by: tackline on April 16, 2004 at 07:11 PM

  • suite
    Why not use the suite method for setup stuff?

    Posted by: hani on April 16, 2004 at 08:19 PM

  • suite
    I just don't tend to use suites, preferring to get my tool (i.e. IDEA or Ant) to run all tests in a particular package/directory.

    Posted by: simongbrown on April 17, 2004 at 12:37 AM

  • File.deleteOnExit?
    I think it's cleaner and quicker to simply blast an entire top-level directory away recursively after the tests have all been run. Also, for some of the tests, Jakarta Lucene is creating index files and I have no idea upfront of what these are called and where they are stored. I used to delete the files between runs and the tests started to take a long time to run in comparison.

    Posted by: simongbrown on April 17, 2004 at 12:43 AM

  • suite
    Both will run a suite just fine. Why write crappy code when the framework handles it for you?

    Posted by: wcrosman on April 19, 2004 at 12:16 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds