如何将JUnitPerf与JWebUnit和JUnit 4结合使用?

时间:2020-03-06 14:22:51  来源:igfitidea点击:

我针对可正确运行的Web应用程序进行了一系列功能测试,但每个功能测试都需要使用@BeforeClass和@AfterClass批注提供的类级别的设置和拆卸,因此需要JUnit 4.0或者更高版本。

现在,我想使用少量的这些功能测试来执行负载测试,这些功能测试可以模拟大量请求Web应用程序相关页面的用户。为了使每个用户在JWebUnit中都有自己的"模拟浏览器",我需要在JUnitPerf中使用TestFactory来实例化测试中的类,但是由于JUnit 4测试是用@Test注释的,而不是从TestCase派生的,我得到一个TestFactory必须用一个TestCase class`异常构造。

是否有人成功将JUnitPerf及其TestFactory与JUnit 4结合使用?使它全部起作用的秘诀是什么?

解决方案

我们需要一个JUnit4感知的TestFactory。我在下面列出了一个。

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

}