有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在JWebUnit和JUnit4中使用JUnitPerf?

我有一系列针对正确运行的web应用程序的功能测试,但是每个测试都需要类级别的设置和带有@BeforeClass@AfterClass注释的拆卸,因此需要JUnit4.0或更高版本

现在,我想使用少量功能测试来执行负载测试,这些测试模拟大量用户请求web应用程序的相关页面。为了让每个用户在JWebUnit中都有自己的“模拟浏览器”,我需要在JUnitPerf中使用TestFactory来实例化被测试的类,但是由于JUnit4测试是用@Test注释的,而不是从TestCase派生的,因此我得到了一个TestFactory must be constructed with a TestCase class异常

有人成功地将JUnitPerf及其TestFactory与JUnit4一起使用了吗?什么是让这一切都起作用的秘方


共 (1) 个答案

  1. # 1 楼答案

    您需要一个支持JUnit4的测试工厂。我在下面加了一个

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