views:

1315

answers:

1

I have a series of functional tests against a web application that correctly run, but each require the class level setup and teardown provided with the @BeforeClass and @AfterClass annotations, and hence require JUnit 4.0 or above.

Now I want to perform load testing using a small number of these functional tests, which simulate a large number of users requesting the related page of the web application. In order for each user to have their own "simulated browser" in JWebUnit, I need to use a TestFactory in JUnitPerf to instantiate the class under test, but since JUnit 4 tests are annotated with @Test instead of being derived from TestCase, I'm getting a "TestFactory must be constructed with a TestCase class" exception.

Is anyone successfully using JUnitPerf and its TestFactory with JUnit 4? And what is the secret sauce that lets it all work?

+7  A: 

You need a JUnit4 aware TestFactory. I've included one below.

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.clarkware.junitperf.TestFactory;

class JUnit4TestFactory extends TestFactory {

    static class DummyTestCase extends TestCase {
     public void test() {
     }
    }

    private Class<?> junit4TestClass;

    public JUnit4TestFactory(Class<?> testClass) {
     super(DummyTestCase.class);
     this.junit4TestClass = testClass;
    }

    @Override
    protected TestSuite makeTestSuite() {
     JUnit4TestAdapter unit4TestAdapter = new JUnit4TestAdapter(this.junit4TestClass);
     TestSuite testSuite = new TestSuite("JUnit4TestFactory");
     testSuite.addTest(unit4TestAdapter);
     return testSuite;
    }

}
alexguev
I kind of expected this answer (and was about to check the source out of git), but you went "above and beyond". Would you mind if I checked your source code in as part of the standard JUnitPerf library (with attribution of cource)?
Steve Moyer
please feel free to do it :)
alexguev