View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.testng;
20  
21  import junit.framework.TestCase;
22  import org.apache.maven.surefire.api.report.CategorizedReportEntry;
23  import org.apache.maven.surefire.api.report.SimpleReportEntry;
24  import org.apache.maven.surefire.api.report.TestOutputReportEntry;
25  import org.apache.maven.surefire.api.report.TestReportListener;
26  import org.mockito.ArgumentCaptor;
27  import org.testng.ITestClass;
28  import org.testng.ITestNGMethod;
29  import org.testng.ITestResult;
30  
31  import static org.assertj.core.api.Assertions.assertThat;
32  import static org.mockito.Mockito.mock;
33  import static org.mockito.Mockito.verify;
34  import static org.mockito.Mockito.verifyNoMoreInteractions;
35  import static org.mockito.Mockito.when;
36  import static org.powermock.reflect.Whitebox.invokeMethod;
37  
38  /**
39   * Tests for {@link TestNGReporter}.
40   */
41  @SuppressWarnings("checkstyle:magicnumber")
42  public class TestNGReporterTest extends TestCase {
43      public void testParameterizedTestName() throws Exception {
44          ITestNGMethod method = mock(ITestNGMethod.class);
45          when(method.getCurrentInvocationCount()).thenReturn(3);
46          ITestResult testResult = mock(ITestResult.class);
47          when(testResult.getName()).thenReturn("myTest");
48          when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
49          when(testResult.getMethod()).thenReturn(method);
50          String testName = invokeMethod(TestNGReporter.class, "testName", testResult);
51          assertThat(testName).isEqualTo("myTest[val1, val2](3)");
52      }
53  
54      public void testWithoutParameterizedTestName() throws Exception {
55          ITestNGMethod method = mock(ITestNGMethod.class);
56          ITestResult testResult = mock(ITestResult.class);
57          when(testResult.getName()).thenReturn("myTest");
58          when(testResult.getMethod()).thenReturn(method);
59          String testName = invokeMethod(TestNGReporter.class, "testName", testResult);
60          assertThat(testName).isEqualTo("myTest");
61      }
62  
63      public void testOnTestStart() {
64          ITestClass cls = mock(ITestClass.class);
65          when(cls.getName()).thenReturn("pkg.MyClass");
66  
67          ITestNGMethod method = mock(ITestNGMethod.class);
68          when(method.getCurrentInvocationCount()).thenReturn(3);
69          when(method.getGroups()).thenReturn(new String[0]);
70  
71          ITestResult testResult = mock(ITestResult.class);
72          when(testResult.getTestClass()).thenReturn(cls);
73          when(testResult.getMethod()).thenReturn(method);
74          when(testResult.getName()).thenReturn("myTest");
75          when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
76  
77          TestReportListener<TestOutputReportEntry> listener = mock(TestReportListener.class);
78          TestNGReporter reporter = new TestNGReporter(listener);
79          reporter.onTestStart(testResult);
80  
81          ArgumentCaptor<CategorizedReportEntry> reportEntry = ArgumentCaptor.forClass(CategorizedReportEntry.class);
82          verify(listener).testStarting(reportEntry.capture());
83          verifyNoMoreInteractions(listener);
84  
85          assertThat(reportEntry.getValue().getTestRunId()).isEqualTo(0x0000000100000001L);
86  
87          assertThat(reportEntry.getValue().getSourceName()).isEqualTo("pkg.MyClass");
88  
89          assertThat(reportEntry.getValue().getName()).isEqualTo("myTest[val1, val2](3)");
90      }
91  
92      public void testOnTestSuccess() {
93          ITestClass cls = mock(ITestClass.class);
94          when(cls.getName()).thenReturn("pkg.MyClass");
95  
96          ITestNGMethod method = mock(ITestNGMethod.class);
97          when(method.getCurrentInvocationCount()).thenReturn(3);
98  
99          ITestResult testResult = mock(ITestResult.class);
100         when(testResult.getTestClass()).thenReturn(cls);
101         when(testResult.getMethod()).thenReturn(method);
102         when(testResult.getName()).thenReturn("myTest");
103         when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
104 
105         TestReportListener<TestOutputReportEntry> listener = mock(TestReportListener.class);
106         TestNGReporter reporter = new TestNGReporter(listener);
107         reporter.onTestSuccess(testResult);
108 
109         ArgumentCaptor<SimpleReportEntry> reportEntry = ArgumentCaptor.forClass(SimpleReportEntry.class);
110         verify(listener).testSucceeded(reportEntry.capture());
111         verifyNoMoreInteractions(listener);
112 
113         assertThat(reportEntry.getValue().getTestRunId()).isEqualTo(0x0000000100000001L);
114 
115         assertThat(reportEntry.getValue().getSourceName()).isEqualTo("pkg.MyClass");
116 
117         assertThat(reportEntry.getValue().getName()).isEqualTo("myTest[val1, val2](3)");
118     }
119 
120     public void testOnTestFailure() {
121         Exception stackTrace = new Exception();
122 
123         ITestClass cls = mock(ITestClass.class);
124         when(cls.getName()).thenReturn(getClass().getName());
125 
126         ITestNGMethod method = mock(ITestNGMethod.class);
127         when(method.getCurrentInvocationCount()).thenReturn(1);
128         when(method.getMethodName()).thenReturn("myTest");
129 
130         ITestResult testResult = mock(ITestResult.class);
131         when(testResult.getThrowable()).thenReturn(stackTrace);
132         when(cls.getRealClass()).thenReturn(getClass());
133         when(testResult.getTestClass()).thenReturn(cls);
134         when(testResult.getMethod()).thenReturn(method);
135         when(testResult.getName()).thenReturn("myTest");
136         when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
137 
138         TestReportListener<TestOutputReportEntry> listener = mock(TestReportListener.class);
139         TestNGReporter reporter = new TestNGReporter(listener);
140         reporter.onTestFailure(testResult);
141 
142         ArgumentCaptor<SimpleReportEntry> reportEntry = ArgumentCaptor.forClass(SimpleReportEntry.class);
143         verify(listener).testFailed(reportEntry.capture());
144         verifyNoMoreInteractions(listener);
145 
146         assertThat(reportEntry.getValue().getTestRunId()).isEqualTo(0x0000000100000001L);
147 
148         assertThat(reportEntry.getValue().getSourceName()).isEqualTo(getClass().getName());
149 
150         assertThat(reportEntry.getValue().getName()).isEqualTo("myTest[val1, val2](1)");
151 
152         assertThat(reportEntry.getValue().getStackTraceWriter()).isNotNull();
153 
154         assertThat(reportEntry.getValue().getStackTraceWriter().getThrowable().getTarget())
155                 .isSameAs(stackTrace);
156     }
157 
158     public void testOnSkippedTest() {
159         Exception stackTrace = new Exception("test skip reason");
160 
161         ITestClass cls = mock(ITestClass.class);
162         when(cls.getName()).thenReturn(getClass().getName());
163 
164         ITestNGMethod method = mock(ITestNGMethod.class);
165         when(method.getCurrentInvocationCount()).thenReturn(1);
166 
167         ITestResult testResult = mock(ITestResult.class);
168         when(testResult.getThrowable()).thenReturn(stackTrace);
169         when(testResult.getTestClass()).thenReturn(cls);
170         when(testResult.getMethod()).thenReturn(method);
171         when(testResult.getName()).thenReturn("myTest");
172         when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
173 
174         TestReportListener<TestOutputReportEntry> listener = mock(TestReportListener.class);
175         TestNGReporter reporter = new TestNGReporter(listener);
176         reporter.onTestSkipped(testResult);
177 
178         ArgumentCaptor<SimpleReportEntry> reportEntry = ArgumentCaptor.forClass(SimpleReportEntry.class);
179         verify(listener).testSkipped(reportEntry.capture());
180         verifyNoMoreInteractions(listener);
181 
182         assertThat(reportEntry.getValue().getTestRunId()).isEqualTo(0x0000000100000001L);
183 
184         assertThat(reportEntry.getValue().getSourceName()).isEqualTo(getClass().getName());
185 
186         assertThat(reportEntry.getValue().getName()).isEqualTo("myTest[val1, val2](1)");
187 
188         assertThat(reportEntry.getValue().getMessage()).isEqualTo(stackTrace.getMessage());
189     }
190 
191     public void testOnTestFailedButWithinSuccessPercentage() {
192         Exception stackTrace = new Exception();
193 
194         ITestClass cls = mock(ITestClass.class);
195         when(cls.getName()).thenReturn(getClass().getName());
196 
197         ITestNGMethod method = mock(ITestNGMethod.class);
198         when(method.getCurrentInvocationCount()).thenReturn(1);
199         when(method.getMethodName()).thenReturn("myTest");
200 
201         ITestResult testResult = mock(ITestResult.class);
202         when(testResult.getThrowable()).thenReturn(stackTrace);
203         when(cls.getRealClass()).thenReturn(getClass());
204         when(testResult.getTestClass()).thenReturn(cls);
205         when(testResult.getMethod()).thenReturn(method);
206         when(testResult.getName()).thenReturn("myTest");
207         when(testResult.getParameters()).thenReturn(new String[] {"val1", "val2"});
208 
209         TestReportListener<TestOutputReportEntry> listener = mock(TestReportListener.class);
210         TestNGReporter reporter = new TestNGReporter(listener);
211         reporter.onTestFailedButWithinSuccessPercentage(testResult);
212 
213         ArgumentCaptor<SimpleReportEntry> reportEntry = ArgumentCaptor.forClass(SimpleReportEntry.class);
214         verify(listener).testSucceeded(reportEntry.capture());
215         verifyNoMoreInteractions(listener);
216 
217         assertThat(reportEntry.getValue().getTestRunId()).isEqualTo(0x0000000100000001L);
218 
219         assertThat(reportEntry.getValue().getSourceName()).isEqualTo(getClass().getName());
220 
221         assertThat(reportEntry.getValue().getName()).isEqualTo("myTest[val1, val2](1)");
222 
223         assertThat(reportEntry.getValue().getStackTraceWriter()).isNotNull();
224 
225         assertThat(reportEntry.getValue().getStackTraceWriter().getThrowable().getTarget())
226                 .isSameAs(stackTrace);
227     }
228 }