1 package test.factory;
2 
3 import static org.testng.Assert.assertEquals;
4 
5 import org.testng.annotations.AfterMethod;
6 import org.testng.annotations.AfterSuite;
7 import org.testng.annotations.BeforeMethod;
8 import org.testng.annotations.BeforeSuite;
9 import org.testng.annotations.Test;
10 
11 import java.util.ArrayList;
12 import java.util.List;
13 
14 /**
15  * Test that setUp methods are correctly interleaved even
16  * when we use similar instances of a same test class.
17  *
18  * @author cbeust
19  */
20 public class Sample2 {
21   private static List<String> m_methodList = new ArrayList<>();
22 
23   @BeforeSuite
init()24   public void init() {
25     m_methodList = new ArrayList<>();
26   }
27 
28   @BeforeMethod
setUp()29   public void setUp() {
30     ppp("SET UP");
31     m_methodList.add("setUp");
32   }
33 
34   @AfterMethod
tearDown()35   public void tearDown() {
36     ppp("TEAR DOWN");
37     m_methodList.add("tearDown");
38   }
39 
40   @AfterSuite
afterSuite()41   public void afterSuite() {
42     String[] expectedStrings = {
43         "setUp",
44         "testInputImages",
45         "tearDown",
46         "setUp",
47         "testInputImages",
48         "tearDown",
49         "setUp",
50         "testImages",
51         "tearDown",
52         "setUp",
53         "testImages",
54         "tearDown",
55     };
56     List<String> expected = new ArrayList<>();
57     for (String s : expectedStrings) {
58       expected.add(s);
59     }
60 
61     ppp("ORDER OF METHODS:");
62     for (String s : m_methodList) {
63       ppp("   " + s);
64     }
65 
66     assertEquals(m_methodList, expected);
67   }
68 
69   @Test
testInputImages()70   public void testInputImages() {
71     m_methodList.add("testInputImages");
72     ppp("TESTINPUTIMAGES");
73   }
74 
75   @Test(dependsOnMethods={"testInputImages"})
testImages()76   public void testImages() {
77     m_methodList.add("testImages");
78   }
79 
ppp(String s)80   private static void ppp(String s) {
81     if (false) {
82       System.out.println("[Sample2] " + s);
83     }
84   }
85 }
86