3

我有一个测试类和测试方法。这取决于数据提供者方法。数据提供者方法位于不同于测试类或其基类之一的类中。根据 TestNG 文档

如果要将数据提供者放在不同的类中,它需要是静态方法或具有非参数构造函数的类,并在 dataProviderClass 属性中指定可以找到它的类

测试类:

package test;

import org.testng.Assert;
import org.testng.annotations.Test;

import dataprovider.TestDataProvider;

public class TestClass {
      @Test(dataProvider = "get", dataProviderClass=TestDataProvider.class)
      public void test1(String s1, String s2) {
          Assert.assertEquals(s1.length(), s2.length());
      }
}

数据提供者类:

package dataprovider;

import org.testng.annotations.DataProvider;

public class TestDataProvider {
    @DataProvider(name = "get")
    public Object[][] testData() {
        return new Object[][] {{"a", "b"},{"c", "d"}};
    }
}

Eclipse 的测试输出:

    java.lang.RuntimeException: Can't invoke method public java.lang.Object[][] dataprovider.
TestDataProvider.testData() on this instance of class test.TestClass due to class mismatch
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:75)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:137)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:430)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1243)
    at org.testng.internal.Invoker.createParameters(Invoker.java:992)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1082)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:778)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
    at org.testng.TestNG.runSuites(TestNG.java:1075)
    at org.testng.TestNG.run(TestNG.java:1047)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

似乎它不起作用。我正在使用 TestNG 6.9.11 并尝试从 Maven 和 Eclipse 运行。我正在使用最新的 Eclipse-TestNG 插件。

4

4 回答 4

7

免责声明:我是从外部 dataProvider 添加非静态方法支持的人。

只需添加@Guice您的课程:

@Guice
public class TestClass {
    @Test(dataProvider = "get", dataProviderClass=TestDataProvider.class)
    public void test1(String s1, String s2) {
        Assert.assertEquals(s1.length(), s2.length());
    }
}

这是一个尚未提及的问题。

于 2016-04-12T09:50:37.737 回答
0
  1. TestNG 首先在同一个类中寻找数据提供者。例如:
public class DummyTest {

  @DataProvider(name = "data")
  public Object[][] dummyData() {
      return new Object[][]{
              {"Khan"}, {"Kapoor"}
      };
  }

  @Test(dataProvider = "data")
  public void dataProviderTest(String name){
      System.out.println("My Name is " + name);
  }
  1. 如果在同一个类中没有找到 dataprovider,那么 TestNG 会在基类中查找。例如:
public class DummyData {

    @DataProvider(name = "data")
    public Object[][] dummyData() {
        return new Object[][]{
                {"Khan"}, {"Kapoor"}
        };
    }
}
public class DummyTest extends DummyData {
    @Test(dataProvider = "data")
    public void dataProviderTest(String name){
        System.out.println("My Name is " + name);
    }
}
  1. 如果您的 DataProvider 具有默认或无参数构造函数,则可以声明数据提供程序方法是静态的或非静态的。例如
public class DummyData {

    @DataProvider(name = "data")
    public Object[][] dummyData() {
        return new Object[][]{
                {"Khan"}, {"Kapoor"}
        };
    }
}
public class DummyTest {
    @Test(dataProvider = "data", dataProviderClass = DummyData.class)
    public void dataProviderTest(String name){
        System.out.println("My Name is " + name);
    }
}
  1. 如果 DataProvider 类具有参数化构造函数,请将 dataprovider 方法声明为静态,否则 jvm 将抛出错误。来自 intellij 的错误快照:
objc[48034]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x1037f04c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1038b84e0). One of the two will be used. Which one is undefined.
[Utils] [ERROR] [Error] java.lang.InstantiationException: apitestset.users.DummyData
    at java.lang.Class.newInstance(Class.java:427)
    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:69)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:582)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:509)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:691)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: java.lang.NoSuchMethodException: apitestset.users.DummyData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 23 more


org.testng.TestNGException: 
Cannot instantiate class apitestset.users.DummyData

    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:71)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:582)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:509)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:691)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: java.lang.InstantiationException: apitestset.users.DummyData
    at java.lang.Class.newInstance(Class.java:427)
    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:69)
    ... 22 more
Caused by: java.lang.NoSuchMethodException: apitestset.users.DummyData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 23 more
于 2019-01-29T16:18:47.930 回答
0

你已经回答了你的问题,

如果要将数据提供者放在不同的类中,它需要是静态方法或具有非参数构造函数的类。

并且根据 Java Design 它不应该起作用,导致从其他类调用方法,该方法应该是静态的,或者我们需要该类的对象。

testData方法不是静态的,因此不能使用类调用,并且您没有创建非参数构造函数,因此无法创建对象(没有对象的非静态方法无法调用)。

希望,你明白我的意思,你得到你的答案:)

于 2016-04-12T08:36:31.570 回答
0

对于收到相同错误但无法按照其他答案修复它们的其他人:

在 Eclipse 中将我的 TestNG 插件从 6.X 升级到 7.0 后,我注意到这个错误开始出现。我尝试遵循其他解决方案,但最终对我有用的唯一方法是完全卸载 TestNG(插件、TestNg Maven 等),然后将更新站点设置为特定版本的 TestNG。

于 2020-02-10T20:26:17.473 回答