2008-09-18 62 views
7

我有一系列的針對該正確運行Web應用程序的功能測試,但分別需要設置有@BeforeClass@AfterClass註解類級別的安裝和拆卸,因而需要的JUnit 4.0或以上。如何將JUnitPerf與JWebUnit和JUnit 4一起使用?

現在我想用一個小數目,這些功能測試,其中模擬大量用戶請求的Web應用程序的相關頁面進行負載測試。爲了讓每個用戶在JWebUnit中擁有自己的「模擬瀏覽器」,我需要使用JUnitPerf中的TestFactory來實例化待測試的類,但由於JUnit 4測試使用@Test進行了註釋,而不是從TestCase派生,所以我獲得TestFactory must be constructed with a TestCase class例外。

是否有人成功使用JUnitPerf及其TestFactory與JUnit 4?什麼是讓所有的工作祕密醬?

回答

10

您需要一個支持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; 
    } 

} 
+0

我有點期待這個答案(並且正在檢查源代碼的git),但你「超越了」。作爲標準JUnitPerf庫的一部分(使用cource屬性),你是否介意我檢查了源代碼? – 2008-09-19 11:02:56

相關問題