views:

85

answers:

2

What is the best way run a lot of integration tests using JUnit?

I crudely discovered that the code below can run all the tests... but it has a massive flaw. The tearDown() method in each of those classes is not called until they have all been run.

public class RunIntegrationTests extends TestSuite {
   public RunIntegrationTests(){
   }

   public static void main (String[] args){
      TestRunner.run(testSuite());
   }

   public static Test testSuite(){

      TestSuite result = new TestSuite();

      result.addTest(new TestSuite(AgreementIntegrationTest.class));
      result.addTest(new TestSuite(InterestedPartyIntegrationTest.class));
      result.addTest(new TestSuite(WorkIntegrationTest.class));
      // further tests omitted for readability

      return result;
   }
}

The classes being run connect to the database, load an object, and display it in a JFrame. I overrode the setVisible method to enable testing. On our build machine, the java vm runs out of memory when running the code above as the objects it has to load from the database are pretty large. If the tearDown() method was called after each class finished it would solve the memory problems.

Is there a better way to run them? I'm having to use JUnit 3.8.2 by the way - we're still on Java 1.4 :(

+1  A: 

Not sure if this is the problem, but according to the JUnit Primer you should just add the tests directly, instead of using the TestSuite:

  result.addTest(new AgreementIntegrationTest()));
  result.addTest(new InterestedPartyIntegrationTest()));
  result.addTest(new WorkIntegrationTest()));
martinus
A: 

That's very strange. setUp and tearDown should bookend the running of each test method, regardless of how the methods are bundled up into suites.

I typically do it slightly differently.

TestSuite suite = new TestSuite( "Suite for ..." ) ;

suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;

And I just verified that tearDown was indeed being called the correct number of times. But your method should work just as well.

Are you sure tearDown is properly specified -- e.g. it's not "teardown"? When you run one test class on its own, is tearDown properly called?

Rodney Gitzel