1 package test.listeners;
2 
3 import org.testng.Assert;
4 import org.testng.TestNG;
5 import org.testng.annotations.BeforeMethod;
6 import org.testng.annotations.Test;
7 import org.testng.collections.Lists;
8 
9 import org.testng.xml.XmlClass;
10 import org.testng.xml.XmlSuite;
11 import org.testng.xml.XmlTest;
12 import test.SimpleBaseTest;
13 
14 import java.util.Arrays;
15 import java.util.Collections;
16 
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.assertj.core.api.Assertions.entry;
19 
20 public class ListenerTest extends SimpleBaseTest {
21 
22   @BeforeMethod
bm()23   public void bm() {
24     SimpleListener.m_list = Lists.newArrayList();
25   }
26 
27   @Test(description = "Ensure that if a listener is present, we get test(), onSuccess()," +
28   		" afterMethod()")
listenerShouldBeCalledBeforeConfiguration()29   public void listenerShouldBeCalledBeforeConfiguration() {
30     TestNG tng = create(OrderedListenerSampleTest.class);
31     tng.run();
32     Assert.assertEquals(SimpleListener.m_list, Arrays.asList(1, 2, 3, 4));
33   }
34 
35   @Test(description = "TESTNG-400: onTestFailure should be called before @AfterMethod")
failureBeforeAfterMethod()36   public void failureBeforeAfterMethod() {
37     TestNG tng = create(FailingSampleTest.class);
38     tng.run();
39     Assert.assertEquals(SimpleListener.m_list, Arrays.asList(4, 5, 6));
40   }
41 
42   @Test(description = "Inherited @Listeners annotations should aggregate")
aggregateListeners()43   public void aggregateListeners() {
44     TestNG tng = create(AggregateSampleTest.class);
45     AggregateSampleTest.m_count = 0;
46     tng.run();
47     Assert.assertEquals(AggregateSampleTest.m_count, 2);
48   }
49 
50   @Test(description = "Should attach only one instance of the same @Listener class per test")
shouldAttachOnlyOneInstanceOfTheSameListenerClassPerTest()51   public void shouldAttachOnlyOneInstanceOfTheSameListenerClassPerTest() {
52     TestNG tng = create(Derived1.class, Derived2.class);
53     BaseWithListener.m_count = 0;
54     tng.run();
55     Assert.assertEquals(BaseWithListener.m_count, 2);
56   }
57 
58   @Test(description = "@Listeners with an ISuiteListener")
suiteListenersShouldWork()59   public void suiteListenersShouldWork() {
60     TestNG tng = create(SuiteListenerSample.class);
61     SuiteListener.start = 0;
62     SuiteListener.finish = 0;
63     tng.run();
64     Assert.assertEquals(SuiteListener.start, 1);
65     Assert.assertEquals(SuiteListener.finish, 1);
66   }
67 
68   @Test(description = "GITHUB-767: ISuiteListener called twice when @Listeners")
suiteListenerInListernersAnnotationShouldBeRunOnce()69   public void suiteListenerInListernersAnnotationShouldBeRunOnce() {
70     TestNG tng = createTests("Suite", SuiteListenerSample2.class);
71     SuiteListener2.start = 0;
72     SuiteListener2.finish = 0;
73     tng.run();
74     Assert.assertEquals(SuiteListener2.start, 1);
75     Assert.assertEquals(SuiteListener2.finish, 1);
76   }
77 
78   @Test(description = "GITHUB-171")
suiteListenersShouldBeOnlyRunOnceWithManyTests()79   public void suiteListenersShouldBeOnlyRunOnceWithManyTests() {
80     TestNG tng = createTests("suite", Derived1.class, Derived2.class);
81     SuiteListener.start = 0;
82     SuiteListener.finish = 0;
83     tng.run();
84     Assert.assertEquals(SuiteListener.start, 1);
85     Assert.assertEquals(SuiteListener.finish, 1);
86   }
87 
88   @Test(description = "GITHUB-795")
suiteListenersShouldBeOnlyRunOnceWithManyIdenticalTests()89   public void suiteListenersShouldBeOnlyRunOnceWithManyIdenticalTests() {
90     TestNG tng = createTests("suite", Derived1.class, Derived1.class);
91     SuiteListener.start = 0;
92     SuiteListener.finish = 0;
93     tng.run();
94     Assert.assertEquals(SuiteListener.start, 1);
95     Assert.assertEquals(SuiteListener.finish, 1);
96   }
97 
98   @Test(description = "GITHUB-169")
invokedMethodListenersShouldBeOnlyRunOnceWithManyTests()99   public void invokedMethodListenersShouldBeOnlyRunOnceWithManyTests() {
100     TestNG tng = createTests("suite", Derived1.class, Derived2.class);
101     MyInvokedMethodListener.beforeInvocation.clear();
102     MyInvokedMethodListener.afterInvocation.clear();
103     tng.run();
104     assertThat(MyInvokedMethodListener.beforeInvocation).containsOnly(
105             entry("t", 1), entry("s", 1)
106     );
107     assertThat(MyInvokedMethodListener.afterInvocation).containsOnly(
108             entry("t", 1), entry("s", 1)
109     );
110   }
111 
112   @Test(description = "GITHUB-154: MethodInterceptor will be called twice")
methodInterceptorShouldBeRunOnce()113   public void methodInterceptorShouldBeRunOnce() {
114     TestNG tng = create(SuiteListenerSample.class);
115     MyMethodInterceptor interceptor = new MyMethodInterceptor();
116     tng.addListener(interceptor);
117     tng.run();
118     Assert.assertEquals(interceptor.getCount(), 1);
119   }
120 
121   @Test(description = "GITHUB-356: Add listeners for @BeforeClass/@AfterClass")
classListenerShouldWork()122   public void classListenerShouldWork() {
123     MyClassListener.beforeNames.clear();
124     MyClassListener.afterNames.clear();
125     TestNG tng = create(Derived1.class, Derived2.class);
126     MyClassListener listener = new MyClassListener();
127     tng.addListener(listener);
128     tng.run();
129     assertThat(MyClassListener.beforeNames).containsExactly("Derived1", "Derived2");
130     assertThat(MyClassListener.afterNames).containsExactly("Derived1", "Derived2");
131   }
132 
133   @Test(description = "GITHUB-356: Add listeners for @BeforeClass/@AfterClass")
classListenerShouldWorkFromAnnotation()134   public void classListenerShouldWorkFromAnnotation() {
135     MyClassListener.beforeNames.clear();
136     MyClassListener.afterNames.clear();
137     TestNG tng = create(ClassListenerSample.class);
138     tng.run();
139     assertThat(MyClassListener.beforeNames).containsExactly("ClassListenerSample");
140     assertThat(MyClassListener.afterNames).containsExactly("ClassListenerSample");
141   }
142 }
143