Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use JUnitPerf with JWebUnit and JUnit 4?

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?

like image 901
Steve Moyer Avatar asked Sep 18 '08 18:09

Steve Moyer


1 Answers

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;
    }

}
like image 198
alexguev Avatar answered Sep 25 '22 02:09

alexguev