Java 使用不同的数据提供程序在TestNG中重复整个测试类
我使用TestNG,我希望在不同的时间运行相同的测试,每次都使用特定的数据提供程序,这些数据提供程序将在测试方法的子集中使用,换句话说,我希望在不更改测试本身的情况下使用不同的数据运行测试类 我在为测试类创建工厂时遇到问题,以下是我的案例: 错误:Java 使用不同的数据提供程序在TestNG中重复整个测试类,java,testng,Java,Testng,我使用TestNG,我希望在不同的时间运行相同的测试,每次都使用特定的数据提供程序,这些数据提供程序将在测试方法的子集中使用,换句话说,我希望在不更改测试本身的情况下使用不同的数据运行测试类 我在为测试类创建工厂时遇到问题,以下是我的案例: 错误: 使用org.apache.maven.surefire.TestNG.conf配置TestNG。TestNGMapConfigurator@18e8568 org.apache.maven.surefire.util.SurefireReflec
使用org.apache.maven.surefire.TestNG.conf配置TestNG。TestNGMapConfigurator@18e8568
org.apache.maven.surefire.util.SurefireReflectionException:java.lang.reflect.InvocationTargetException;嵌套异常为java.lang.reflect.InvocationTargetException:null
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:483)
位于org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
位于org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
位于org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
位于org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
位于org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
原因:org.testng.TestNGException:
工厂方法类MyTest.createInstances()引发了异常
位于org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:92)
位于org.testng.internal.TestNGClassFinder(TestNGClassFinder.java:140)
位于org.testng.TestRunner.initMethods(TestRunner.java:405)
位于org.testng.TestRunner.init(TestRunner.java:231)
位于org.testng.TestRunner.init(TestRunner.java:201)
位于org.testng.TestRunner.(TestRunner.java:150)
位于org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:523)
位于org.testng.SuiteRunner.init(SuiteRunner.java:157)
位于org.testng.SuiteRunner(SuiteRunner.java:111)
位于org.testng.testng.createSuiteRunner(testng.java:1212)
位于org.testng.testng.createSuiteRunners(testng.java:1199)
位于org.testng.testng.runSuitesLocally(testng.java:1053)
位于org.testng.testng.run(testng.java:974)
位于org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
位于org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110)
位于org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
... 9更多
原因:java.lang.NullPointerException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:483)
位于org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:80)
... 还有24个
此外,DataTest
组件目前包含两个参数,但它将包含更多的参数(用于定义预期值)和一组数据提供程序
最后,如果我使用一个字符串作为测试类的参数(作为一种尝试),测试将运行。问题是您无法实例化
MyTest
的新实例来调用createInstances
。所以这就成了鸡和蛋的问题。如果将createInstances
声明为静态方法,那么它应该可以工作 这看起来像是一个TestNG问题。您使用的是哪个版本?你能试试最新的版本(6.9.9)吗?那么,是否可以在上创建问题?我使用6.3.1版本。此外,我尝试了最新版本(6.9.9),但我遇到了同样的问题。我将在GitHub上创建一个问题…相关问题:它工作正常,现在我将尝试完成整个过程。非常感谢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: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@18e8568
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