使用不同的数据提供程序在TestNG中重复整个测试类

时间:2015-11-12 16:36:39

标签: java 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: 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

此外,DataTest组件目前包含两个参数,但它将包含更多参数 - 用于定义预期值 - 以及一组数据提供者。

最后,如果我使用String作为测试类的参数 - 就像尝试一样 - 测试运行。

1 个答案:

答案 0 :(得分:2)

问题是您无法实例化MyTest的新实例以便调用createInstances。所以它成了鸡和蛋的问题。如果您将createInstances声明为静态方法,它应该可以正常工作。