I am using TestNG @DataProvider
Annotation along with @Factory
functionality in order to run a flow. When the control goes to @DataProvider
method, I want to know from which test class this dataprovider method was called & on basis of that I want to supply test data.
This is my Bean class:
public class Bean {
private String fName = null;
private String lName = null;
private String address = null;
public synchronized String getfName() {
return fName;
}
public synchronized void setfName(String fName) {
this.fName = fName;
}
public synchronized String getlName() {
return lName;
}
public synchronized void setlName(String lName) {
this.lName = lName;
}
public synchronized String getAddress() {
return address;
}
public synchronized void setAddress(String address) {
this.address = address;
}
}
This is my constructor of TestClass with @Factory
@Factory(dataProvider = "dataProvider")
public TestClass(Bean bean) {
this.fName = bean.getfName();
this.lName = bean.getlName();
this.address = bean.getAddress();
}
Here is my DataProvider:
@DataProvider(name = "dataProvider")
public static Object[][] dataProvider() {
Bean oneBean = new Bean();
oneBean.setfName("Nisarg");
oneBean.setlName("Dave");
oneBean.setAddress("India");
Bean secondBean = new Bean();
secondBean.setfName("ABCD");
secondBean.setlName("XYZ");
secondBean.setAddress("UK");
return new Object[][] {
new Object[] { oneBean },
new Object[] { secondBean }
};
}
Here in my DataProvider I know I can pass Method m or ITestContext
as parameters, but both of them are not giving me Test class Name from which it was called.
Can anyone please help on this?
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.ITestNGMethod;
import org.testng.annotations.DataProvider;
public class DataProviderHouse {
@DataProvider(name = "dataProvider")
public static Object[][] dataProvider(ITestNGMethod method) {
Class clazz = method.getRealClass();
Method tm = method.getConstructorOrMethod().getMethod();
Constructor constructor = method.getConstructorOrMethod().getConstructor();
String name = clazz.getName() + ".";
if (tm == null) {
name = constructor.getName();
} else {
name = name + tm.getName();
}
System.err.println("Data provider is being invoked by " + name);
Bean oneBean = new Bean("Master Oogway", 75);
Bean secondBean = new Bean("Master Shifu", 60);
return new Object[][] {new Object[] {oneBean}, new Object[] {secondBean}};
}
}
public class Bean {
private final String name;
private final int age;
public Bean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import org.testng.Assert;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class TestClass {
private final String name;
private final int age;
@Factory(dataProvider = "dataProvider", dataProviderClass = DataProviderHouse.class)
public TestClass(Bean bean) {
this.name = bean.getName();
this.age = bean.getAge();
}
@Test
public void testName() {
Assert.assertNotNull(name);
}
@Test
public void testAge() {
Assert.assertTrue(age > 0 && age <= 100);
}
@Override
public String toString() {
return "TestClass{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-dev/45720e34-a52e-4cea-895d-1b646980ab4a%40googlegroups.com.
You know, you are awesome ?
Please do know that you have helped me to solve an issue in which I was stuck by a week or so. Thanks a ton !
Because of you guys, the community believes in to hack around TestNG!
Thanks very much !
Regards,
Nisarg