mikemil
mikemil

Reputation: 1241

Switching ActiveTestSuite to JUnit4 with annotations

I have created a some testcases and use an ActiveTestSuite to run the methods in parallel to check out some potential threading issues. Next, I wanted to swap the class over to JUnit4 with the annotation because I need to take advantage of the @BeforeClass and @AfterClass annotations. My problem comes in with creating the test suite. Previously, I did the following

public static Test suite() {
       TestSuite suite = new ActiveTestSuite(
              MultithreadedTests.class.getName());
       // the parm is the method name!!!
       suite.addTest(new MultithreadedTests("testTransferToHostStockCountAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostRTVAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostTRFAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostRCVAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostCartonAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostInvAdjustmentAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostStoreOrderAndFinFile"));
       return suite;
}

In order to get the Before/After class parts working, my test class no longer extends TestCase, otherwise those methods where not getting triggered. I can get my class compiled doing something like this:

package com.jda.portfolio.hostexport.server.exporthandler;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ com.jda.portfolio.hostexport.server.exporthandler.TestThreadedExportHandler.class  })
public class AllTests {} 

But the tests get single-threaded, which is not what I want. I need to be able to run each of the testcase methods in a separate thread but I am having a hard time understanding how to use the JUnit4 annotations to create the suite such that it is an ‘ActiveTestSuite’.

What am I missing?

Upvotes: 2

Views: 1093

Answers (1)

centic
centic

Reputation: 15872

ActiveTestSuite seems to be still available as part of junit 4 in the package "junit.extensions".

There are also some third party extensions which do similar things. I have used a variant of these 3 classes in one of my projects successfully. Also the discussion about Concurrent JUnit Tests With RunnerScheduler sounds like a possible solution.

Upvotes: 1

Related Questions