You want to run several tests concurrently using threads.
Use the junit.extensions.ActiveTestSuite class to build a suite of tests that run concurrently.
The ActiveTestSuite class runs each of its tests in a separate thread. The suite does not finish until all of the test threads are complete. Example 4-6 shows how to run three different test methods in three different threads.
public static Test suite( ) { TestSuite suite = new ActiveTestSuite( ); suite.addTest(new TestGame("testCreateFighter")); suite.addTest(new TestGame("testGameInitialState")); suite.addTest(new TestGame("testSameFighters")); return suite; }
While you probably won't use this technique often, running tests in threads can serve as a rudimentary stress tester. You might also use ActiveTestSuite to help identify threading problems in your code. By combining ActiveTestSuite with RepeatedTest, you can uncover threading problems that only show up intermittently.
Example 4-7 shows how you can combine repeated tests and other test suites into an ActiveTestSuite. Each of the repeated tests runs in a different thread; therefore, you end up with four threads. If you are experiencing occasional threading glitches, you might want to increase the number of iterations and run a similar test suite overnight.
public static Test suite( ) { TestSuite suite = new ActiveTestSuite( ); // run one test in a thread suite.addTest(new TestGame("testCreateFighter")); // run this test 100 times in a second thread suite.addTest(new RepeatedTest( new TestGame("testGameInitialState"), 100)); // run this test 200 times in a third thread suite.addTest(new RepeatedTest( new TestGame("testSameFighters"), 200)); // run some other test suite in a fourth thread suite.addTest(TestPerson.suite( )); return suite; }
Recipe 4.10 explains the RepeatedTest class.