2015-11-12 37 views
1

我使用TestNG並且我想運行不同時間的相同測試,並且每次使用特定的數據提供者,這些提供者將用在測試方法的子集中,換句話說,我想用不同的數據運行測試類而不改變測試本身。在TestNG中重複整個測試類,使用不同的數據提供者

我有創造的工廠測試類的問題,這裏是我的情況:

@Test 
public class MyTest { 

    @Factory 
    public Object[] createInstances() { 
     DataTest dataTest_1 = new DataTest("foo", true); 
     DataTest dataTest_2 = new DataTest("FOO", false); 

     Object[] result = new Object[]{ 
       new MyTest(dataTest_1); 
       new MyTest(dataTest_2) 
     }; 
     return result; 
    } 

    private final DataTest dataTest; 

    public MyTest(DataTest dataTest) { 
     this.dataTest = dataTest; 
    } 
} 

錯誤:

Configuring TestNG with: [email protected]568 
org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null 
java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:483) 
     at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 
     at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 
     at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 
     at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) 
Caused by: org.testng.TestNGException: 
The factory method class MyTest.createInstances() threw an exception 
     at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:92) 
     at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:140) 
     at org.testng.TestRunner.initMethods(TestRunner.java:405) 
     at org.testng.TestRunner.init(TestRunner.java:231) 
     at org.testng.TestRunner.init(TestRunner.java:201) 
     at org.testng.TestRunner.<init>(TestRunner.java:150) 
     at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:523) 
     at org.testng.SuiteRunner.init(SuiteRunner.java:157) 
     at org.testng.SuiteRunner.<init>(SuiteRunner.java:111) 
     at org.testng.TestNG.createSuiteRunner(TestNG.java:1212) 
     at org.testng.TestNG.createSuiteRunners(TestNG.java:1199) 
     at org.testng.TestNG.runSuitesLocally(TestNG.java:1053) 
     at org.testng.TestNG.run(TestNG.java:974) 
     at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77) 
     at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110) 
     at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106) 
     ... 9 more 
Caused by: java.lang.NullPointerException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:483) 
     at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:80) 
     ... 24 more 

而且,此刻DataTest組件包含兩個參數但它將包含更多的參數 - 定義期望值 - 以及數據提供者的集合。

最終,如果我使用String作爲測試類的參數 - 就像嘗試 - 測試運行一樣。

+0

它看起來像一個TestNG問題。你正在使用哪個版本?你可以試試最新版本(6.9.9)嗎?那麼,可以在http://github.com/cbeust/testng上創建一個問題? – juherr

+0

我使用6.3.1版本。此外,我已經嘗試了最新版本(6.9.9),但我遇到了同樣的問題。我將在GitHub上創建一個問題... – vimterd

+0

相關問題:https://github.com/cbeust/testng/issues/876 – juherr

回答

2

問題是,您無法實例化MyTest的新實例,以致電createInstances。所以它變成了雞和雞蛋的問題。如果你聲明createInstances是一個靜態方法,它應該工作。

+0

它工作正常,現在我將嘗試完成整個過程。非常感謝 – vimterd

+0

僅供參考,非靜態方法工廠在測試類之外由TestNG支持。 – juherr

相關問題