import java.lang.reflect.Method;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class ChildTestClass1 {
String a;
String b;
String c;
public ChildTestClass1(){}
@Factory(dataProvider = "dp")
public ChildTestClass1(String a1, String a2, String a3) {
a=a1;
b=a2;
c=a3;
}
@DataProvider(name = "dp", parallel = true)
public static Object[][] simpledataProvider(ITestContext context, ITestNGMethod method) throws Exception {
Object[][] data = new Object[][] {
{ "DataA1", "DataA2", "DataA3" },
{ "DataB1", "DataB2", "DataB3" } };
return data;
}
@BeforeMethod()
public synchronized void init(Method m) throws Exception {
Thread.sleep(150);
System.out.println("before method:"+ m.getName());
System.out.println("Current thread id = " + Thread.currentThread().getId());
System.out.println("param a = " + this.a);
System.out.println("param b = " + this.b);
System.out.println("param c = " + this.c);
System.out.println("end Before method\n\n");
}
@Test(dataProvider = "dp")
public void TestMethod1(String a, String b, String c) throws InterruptedException {
synchronized (this) {
Thread.sleep(100);
System.out.println("Start test Method: TestMethod1");
System.out.println("param a = " + a);
System.out.println("param b = " + b);
System.out.println("param c = " + c);
System.out.println("Thread id = " + Thread.currentThread().getId());
System.out.println("end Test method: TestMethod1 \n\n\n");
}
}
}
When this code is executed, instead of getting TestMethod1 with 2 iterations, I'm getting 4.
Seems like it is executed twice from the @Factory, and then twice from the @Dataprovider
All I need is to initialize the class fields so that the @BeforeMethod will be able to process stuff