1 package test.factory;
2 
3 import org.testng.annotations.BeforeTest;
4 import org.testng.annotations.Factory;
5 import org.testng.annotations.Test;
6 
7 /**
8  * this is like the FactoryTest, except it creates test instances in a separate
9  * class from the test class
10  */
11 public class FactoryInSeparateClass {
12   static private boolean m_wasRun = false;
13   static private int m_checkSum = 0;
14 
addToSum(int i)15   public static void addToSum(int i) {
16     m_checkSum += i;
17   }
18 
19   @BeforeTest
beforeTest()20   public void beforeTest() {
21     m_wasRun = false;
22     m_checkSum = 0;
23   }
24 
25   @Factory
createObjects()26   public Object[] createObjects() {
27     return new Object[] {
28       new MyTest(1),
29       new MyTest(2),
30       new MyTest(3),
31     };
32   }
33 
34    @Test(groups = "testMethodOnFactoryClass", dependsOnGroups={"MyTest"})
checkSum()35     public void checkSum() {
36     m_wasRun = true;
37       assert (m_checkSum == 6) :
38         "Test instances made by factory did not invoke their test methods correctly.  expected 6 but got " + m_checkSum;
39     }
40 
wasRun()41     public static boolean wasRun() {
42       return m_wasRun;
43     }
44 }
45 
46