I have a test class and test method. This depends on data provider method. The data provider method is in different class other than the test class or one of its base classes. As per TestNG documentation
If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute
Test class:
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());
}
}
Data provider class:
package dataprovider;
import org.testng.annotations.DataProvider;
public class TestDataProvider {
@DataProvider(name = "get")
public Object[][] testData() {
return new Object[][] {{"a", "b"},{"c", "d"}};
}
}
Test Output from 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)
It seems it's not working. I am using TestNG 6.9.11 and tried running from maven as well as from eclipse. I am using latest Eclipse-TestNG plugin.
Disclaimer: I'm the one who added the support of non static methods from external dataProvider.
Just add @Guice
on your class:
@Guice
public class TestClass {
@Test(dataProvider = "get", dataProviderClass=TestDataProvider.class)
public void test1(String s1, String s2) {
Assert.assertEquals(s1.length(), s2.length());
}
}
It is a not yet referenced issue.