1 package test.interleavedorder;
2 
3 import org.testng.Assert;
4 import org.testng.TestListenerAdapter;
5 import org.testng.TestNG;
6 import org.testng.annotations.BeforeTest;
7 import org.testng.annotations.Test;
8 
9 import test.BaseTest;
10 import testhelper.OutputDirectoryPatch;
11 
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 
16 public class InterleavedInvocationTest extends BaseTest {
17   public static List<String> LOG = new ArrayList<>();
18 
19   @BeforeTest
beforeTest()20   public void beforeTest() {
21     LOG = new ArrayList<>();
22   }
23 
verifyInvocation(int number, List<String> log, int index)24   private void verifyInvocation(int number, List<String> log, int index) {
25     Assert.assertEquals(log.get(index), "beforeTestChild" + number + "Class");
26     Assert.assertTrue(("test1".equals(log.get(index + 1)) && "test2".equals(LOG.get(index + 2)))
27         || ("test2".equals(LOG.get(index + 1)) && "test1".equals(LOG.get(index + 2))),
28         "test methods were not invoked correctly");
29     Assert.assertEquals(log.get(index + 3), "afterTestChild" + number + "Class");
30   }
31 
32   @Test
invocationOrder()33   public void invocationOrder() {
34     TestListenerAdapter tla = new TestListenerAdapter();
35     TestNG testng = new TestNG();
36     testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
37     testng.setTestClasses(new Class[] { TestChild1.class, TestChild2.class });
38     testng.addListener(tla);
39     testng.setVerbose(0);
40     testng.run();
41 
42     Assert.assertEquals(LOG.size(), 8, LOG.toString());
43     int number1 = "beforeTestChild1Class".equals(LOG.get(0)) ? 1 : 2;
44     int number2 = number1 == 1 ? 2 : 1;
45     verifyInvocation(number1, LOG, 0);
46     verifyInvocation(number2, LOG, 4);
47   }
48 
ppp(String s)49   public static void ppp(String s) {
50     System.out.println("[InterleavedTest] " + s);
51   }
52 }
53