1 package test.invokedmethodlistener;
2 
3 import org.testng.Assert;
4 import org.testng.IInvokedMethod;
5 import org.testng.IInvokedMethodListener;
6 import org.testng.ITestResult;
7 import org.testng.TestNG;
8 import org.testng.annotations.Test;
9 import org.testng.xml.XmlSuite;
10 
11 import test.SimpleBaseTest;
12 
13 import java.util.List;
14 
15 public class InvokedMethodListenerTest extends SimpleBaseTest {
16 
run(Class[] classes, IInvokedMethodListener l)17   private static void run(Class[] classes, IInvokedMethodListener l) {
18     TestNG tng = create();
19     tng.setTestClasses(classes);
20 
21     tng.addInvokedMethodListener(l);
22     tng.run();
23   }
24 
assertMethodCount(MyListener l)25   private static void assertMethodCount(MyListener l) {
26     Assert.assertEquals(l.getBeforeCount(), 9);
27     Assert.assertEquals(l.getAfterCount(), 9);
28   }
29 
30   @Test
withSuccess()31   public void withSuccess() {
32     MyListener l = new MyListener();
33     run(new Class[]{Success.class}, l);
34     assertMethodCount(l);
35   }
36 
37   @Test
withFailure()38   public void withFailure() {
39     MyListener l = new MyListener();
40     run(new Class[] { Failure.class }, l);
41     assertMethodCount(l);
42     Assert.assertEquals(l.getSuiteStatus(), ITestResult.FAILURE);
43     Assert.assertTrue(null != l.getSuiteThrowable());
44     Assert.assertTrue(l.getSuiteThrowable().getClass() == RuntimeException.class);
45 
46     Assert.assertEquals(l.getMethodStatus(), ITestResult.FAILURE);
47     Assert.assertTrue(null != l.getMethodThrowable());
48     Assert.assertTrue(l.getMethodThrowable().getClass() == IllegalArgumentException.class);
49   }
50 
51   /**
52    * Fix for:
53    * https://github.com/juherr/testng-googlecode/issues/7
54    * https://github.com/juherr/testng-googlecode/issues/86
55    * https://github.com/cbeust/testng/issues/93
56    */
57   @Test
sameMethodInvokedMultipleTimesShouldHaveDifferentTimeStamps()58   public void sameMethodInvokedMultipleTimesShouldHaveDifferentTimeStamps() {
59     TestNG tng = create(Sample.class);
60     InvokedMethodListener listener = new InvokedMethodListener();
61     tng.addListener(listener);
62     tng.run();
63     List<IInvokedMethod> m = listener.getInvokedMethods();
64     IInvokedMethod beforeSuite = m.get(0);
65     Assert.assertFalse(beforeSuite.getTestMethod().isAfterMethodConfiguration());
66     Assert.assertTrue(beforeSuite.isConfigurationMethod());
67     IInvokedMethod after1 = m.get(2);
68     Assert.assertTrue(after1.getTestMethod().isAfterMethodConfiguration());
69     Assert.assertTrue(after1.isConfigurationMethod());
70     IInvokedMethod after2 = m.get(4);
71     Assert.assertTrue(after2.getTestMethod().isAfterMethodConfiguration());
72     Assert.assertTrue(after2.isConfigurationMethod());
73     Assert.assertTrue(after1.getDate() != after2.getDate());
74   }
75 
76   @Test(description = "Test methods with expected exceptions should show up as pass" +
77   		" in IInvokedMethodListener's afterInvocation method")
testMethodsWithExpectedExceptionsShouldShowUpAsPass()78   public void testMethodsWithExpectedExceptionsShouldShowUpAsPass() {
79     TestNG tng = create(Sample2.class);
80     Sample2.Sample2InvokedMethodListener l = new Sample2().new Sample2InvokedMethodListener();
81     tng.addListener(l);
82     tng.run();
83 
84     Assert.assertTrue(l.isSuccess);
85   }
86 
87   @Test(description = "Invoked method does not recognize configuration method")
issue629_InvokedMethodDoesNotRecognizeConfigurationMethod()88   public void issue629_InvokedMethodDoesNotRecognizeConfigurationMethod() {
89     InvokedMethodNameListener l = new InvokedMethodNameListener();
90     run(new Class[]{Success.class}, l);
91 
92     Assert.assertEquals(l.testMethods.size(), 1);
93     Assert.assertTrue(l.testMethods.contains("a"));
94 
95     Assert.assertEquals(l.testMethodsFromTM.size(), 1);
96     Assert.assertTrue(l.testMethodsFromTM.contains("a"));
97 
98     Assert.assertEquals(l.configurationMethods.size(), 8);
99     Assert.assertTrue(l.configurationMethods.contains("beforeMethod"));
100     Assert.assertTrue(l.configurationMethods.contains("afterMethod"));
101     Assert.assertTrue(l.configurationMethods.contains("beforeTest"));
102     Assert.assertTrue(l.configurationMethods.contains("afterTest"));
103     Assert.assertTrue(l.configurationMethods.contains("beforeClass"));
104     Assert.assertTrue(l.configurationMethods.contains("afterClass"));
105     Assert.assertTrue(l.configurationMethods.contains("beforeSuite"));
106     Assert.assertTrue(l.configurationMethods.contains("afterSuite"));
107 
108     Assert.assertEquals(l.configurationMethodsFromTM.size(), 8);
109     Assert.assertTrue(l.configurationMethodsFromTM.contains("beforeMethod"));
110     Assert.assertTrue(l.configurationMethodsFromTM.contains("afterMethod"));
111     Assert.assertTrue(l.configurationMethodsFromTM.contains("beforeTest"));
112     Assert.assertTrue(l.configurationMethodsFromTM.contains("afterTest"));
113     Assert.assertTrue(l.configurationMethodsFromTM.contains("beforeClass"));
114     Assert.assertTrue(l.configurationMethodsFromTM.contains("afterClass"));
115     Assert.assertTrue(l.configurationMethodsFromTM.contains("beforeSuite"));
116     Assert.assertTrue(l.configurationMethodsFromTM.contains("afterSuite"));
117   }
118 
119   @Test
issue87_method_orderning_with_disable_test_class()120   public void issue87_method_orderning_with_disable_test_class() {
121     assertIssue87(A.class, B.class, C.class);
122     assertIssue87(A.class, C.class, B.class);
123     assertIssue87(B.class, A.class, C.class);
124   }
125 
assertIssue87(Class<?>.... tests)126   private void assertIssue87(Class<?>... tests) {
127     TestNG tng = create(tests);
128     tng.setParallel(XmlSuite.ParallelMode.FALSE);
129     tng.setPreserveOrder(true);
130     InvokedMethodListener listener = new InvokedMethodListener();
131     tng.addListener(listener);
132     tng.run();
133     List<IInvokedMethod> m = listener.getInvokedMethods();
134     Assert.assertEquals(m.get(0).getTestMethod().getMethodName(), "someMethod1");
135     Assert.assertEquals(m.get(1).getTestMethod().getMethodName(), "someMethod3");
136     Assert.assertEquals(m.get(2).getTestMethod().getMethodName(), "someTest");
137     Assert.assertEquals(m.size(), 3);
138   }
139 }
140