Closed galactoise closed 4 years ago
@galactoise - Please add @NoInjection
annotation to your method parameter of your data driven test, to ensure that TestNG disables the native injection and instead relies on the data provider to feed in the test method. Also please make sure that you are using 7.0.0
(latest released version of TestNG)
Here's a sample that demonstrates
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.NoInjection;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class DataProviderWithMethodTest {
public void myMethod() {}
@Test //Passes when no DataProvider used
public void testMethodWithoutDataProvider() throws NoSuchMethodException, SecurityException {
Method m = DataProviderWithMethodTest.class.getMethod("myMethod");
assertEquals(m.getName(),"myMethod");
}
@DataProvider //References Method object in exact same way as test above
public Object[][] methodProvider() throws NoSuchMethodException, SecurityException {
return new Object[][] {
{DataProviderWithMethodTest.class.getMethod("myMethod")}
};
}
@Test(dataProvider="methodProvider") //Fails unexpectedly
public void testMethodWithDataProvider(@NoInjection Method m) {
assertEquals(m.getName(),"myMethod");
}
}
Hi @krmahadevan, that's a neat feature, and as I noted, it was exceedingly hard to google whether this was intended behavior or not because of the way the terminology is overloaded. So, I'm cool with this being closed as "by design". A couple notes, though:
@Test
annotated methods, which is untrue per the example above.
TestNG Version
6.14.3
Expected behavior
Objects injected from a DataProvider are passed verbatim without modification.
Actual behavior
Objects of type Method.class, which are added to the DataProvider's Object[][], end up being dropped and replaced with a different instance of Method describing the test method about to be invoked. Additionally, the following warning is printed:
Is the issue reproductible on runner?
Test case sample
Note: The fact that all of the terminology here is overloaded makes it exceedingly impossible to search for other people who have had this issue in the past.