TestNG非静态数据提供程序方法不起作用

时间:2016-04-12 06:19:15

标签: java testng testng-dataprovider testng-eclipse

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

  

如果要将数据提供程序放在不同的类中,则需要使用静态方法或具有非arg构造函数的类,并指定可在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 个答案:

答案 0 :(得分:4)

免责声明:我是added the support of non static methods from external dataProvider

的人

只需在课堂上添加<?php define('HOST','localhost'); define('USER','root'); define('PASS',''); define('DB','farmer'); $conn = mysqli_connect(HOST,USER,PASS,DB); if( !$conn ) { //required post params is missing $response["error"] = TRUE; $response["error_msg"] = "Connection could not be established!"; echo json_encode($response); } $sql = "select code,name,mobile from reg_farmer where dist_name=$_POST[dt_name]"; $res = mysqli_query($conn,$sql); $num_rows = mysqli_num_rows($res); ///echo "$num_rows Rows\n"; if ($num_rows == 0){ $response["error"] = TRUE; $response["error_msg"] = "No Data Exist"; echo json_encode($response); exit(0); } $result = array(); while($row = mysqli_fetch_array($res)){ $result = array("error" => FALSE); $b[]=array('code'=>$row[0],'name'=>$row[1],'mobile'=>$row[2]); $result["District"] = $b; } echo json_encode($result); mysqli_close($conn); ?>

@Guice

这是一个尚未引用的问题。

答案 1 :(得分:0)

你已经回答了你的问题,

  

如果您想将数据提供者放在不同的类中,则需要   是一个静态方法或一个非arg构造函数的类。

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

testData方法不是静态的,所以它不能使用类调用,而且你不是在创建非arg构造函数,因此无法创建对象(没有对象非静态方法无法调用)。

希望,你理解我的观点并得到答案:)

答案 2 :(得分: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.&lt;init&gt;()
    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

答案 3 :(得分:0)

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

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