I try to use the @Factory to run the same Class with multiple instances. Please be noted that I'm a beginner to TestNG.
For e.g:
Implementation of @Factory:
public class MainFactoryClass{
@Factory
public Object[] mainFactory() {
Object[] data = new Object[3];
data[0] = new MainImpClass(9);
data[1] = new MainImpClass(10);
data[2] = new MainImpClass(11);
return data;
}
}
And the main class:
public class MainImpClass {
int a;
public MainImpClass(int a) {
this.a = a;
}
@Test
public void getValue1() {
System.out.println("Value from getValue1: " + a);
}
@Test
public void getValue2() {
System.out.println("Value from getValue2: " + a);
}
@Test
public void getValue3() {
System.out.println("Value from getValue3: " + a);
}
}
Actual:
Value from getValue1: 9
Value from getValue2: 9
Value from getValue3: 9
Value from getValue1: 11
Value from getValue2: 11
Value from getValue3: 11
Value from getValue1: 10
Value from getValue2: 10
Value from getValue3: 10
testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Factory Suite">
<test thread-count="5" name=" Factory Test" group-by-
instances="true">
<classes>
<class name="com.trial.MainFactoryClass"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Updated - Implementing the IMethodInterceptor:
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> list,
ITestContext iTestContext) {
Map<Integer, IMethodInstance> orders = new TreeMap<>(); //
Ordered MAP
for (IMethodInstance instance : list) {
MainImpClass testData = (MainImpClass)
instance.getInstance();
orders.put(Integer.valueOf(testData.getA()), instance);
}
List<IMethodInstance> orderList = new
ArrayList<IMethodInstance>(list.size());
for (Integer order : orders.keySet()) { // rearrange
IMethodInstance test = orders.get(order);
orderList.add(test);
}
return orderList; // TestNG will execute in the order
return List
}
When I try to run it as TestNG, The results are not in the same order we passed from Factory. How do we ensure that the output is same as how we passed the values in?.