12
12
13
13
import static org .assertj .core .api .Assertions .assertThat ;
14
14
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
15
+ import static org .mockito .Mockito .inOrder ;
15
16
import static org .mockito .Mockito .mock ;
16
17
17
18
import java .util .ArrayList ;
33
34
import org .junit .platform .launcher .TestIdentifier ;
34
35
import org .junit .platform .launcher .TestPlan ;
35
36
import org .junit .platform .launcher .core .CompositeTestExecutionListener .EagerTestExecutionListener ;
37
+ import org .mockito .InOrder ;
36
38
37
39
@ TrackLogRecords
38
40
class CompositeTestExecutionListenerTests {
@@ -41,47 +43,37 @@ class CompositeTestExecutionListenerTests {
41
43
42
44
@ Test
43
45
void shouldNotThrowExceptionButLogIfDynamicTestRegisteredListenerMethodFails (LogRecordListener logRecordListener ) {
44
- var testIdentifier = getSampleMethodTestIdentifier ();
45
-
46
- compositeTestExecutionListener ().dynamicTestRegistered (testIdentifier );
46
+ compositeTestExecutionListener ().dynamicTestRegistered (anyTestIdentifier ());
47
47
48
48
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class ,
49
49
"dynamicTestRegistered" );
50
50
}
51
51
52
52
@ Test
53
53
void shouldNotThrowExceptionButLogIfExecutionStartedListenerMethodFails (LogRecordListener logRecordListener ) {
54
- var testIdentifier = getSampleMethodTestIdentifier ();
55
-
56
- compositeTestExecutionListener ().executionStarted (testIdentifier );
54
+ compositeTestExecutionListener ().executionStarted (anyTestIdentifier ());
57
55
58
56
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class , "executionStarted" );
59
57
}
60
58
61
59
@ Test
62
60
void shouldNotThrowExceptionButLogIfExecutionSkippedListenerMethodFails (LogRecordListener logRecordListener ) {
63
- var testIdentifier = getSampleMethodTestIdentifier ();
64
-
65
- compositeTestExecutionListener ().executionSkipped (testIdentifier , "deliberately skipped container" );
61
+ compositeTestExecutionListener ().executionSkipped (anyTestIdentifier (), "deliberately skipped container" );
66
62
67
63
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class , "executionSkipped" );
68
64
}
69
65
70
66
@ Test
71
67
void shouldNotThrowExceptionButLogIfExecutionFinishedListenerMethodFails (LogRecordListener logRecordListener ) {
72
- var testIdentifier = getSampleMethodTestIdentifier ();
73
-
74
- compositeTestExecutionListener ().executionFinished (testIdentifier , mock (TestExecutionResult .class ));
68
+ compositeTestExecutionListener ().executionFinished (anyTestIdentifier (), anyTestExecutionResult ());
75
69
76
70
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class , "executionFinished" );
77
71
}
78
72
79
73
@ Test
80
74
void shouldNotThrowExceptionButLogIfReportingEntryPublishedListenerMethodFails (
81
75
LogRecordListener logRecordListener ) {
82
- var testIdentifier = getSampleMethodTestIdentifier ();
83
-
84
- compositeTestExecutionListener ().reportingEntryPublished (testIdentifier , ReportEntry .from ("one" , "two" ));
76
+ compositeTestExecutionListener ().reportingEntryPublished (anyTestIdentifier (), ReportEntry .from ("one" , "two" ));
85
77
86
78
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class ,
87
79
"reportingEntryPublished" );
@@ -90,10 +82,7 @@ void shouldNotThrowExceptionButLogIfReportingEntryPublishedListenerMethodFails(
90
82
@ Test
91
83
void shouldNotThrowExceptionButLogIfTesPlanExecutionStartedListenerMethodFails (
92
84
LogRecordListener logRecordListener ) {
93
- var testDescriptor = getDemoMethodTestDescriptor ();
94
-
95
- compositeTestExecutionListener ().testPlanExecutionStarted (
96
- TestPlan .from (Set .of (testDescriptor ), mock (ConfigurationParameters .class )));
85
+ compositeTestExecutionListener ().testPlanExecutionStarted (anyTestPlan ());
97
86
98
87
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class ,
99
88
"testPlanExecutionStarted" );
@@ -102,10 +91,7 @@ void shouldNotThrowExceptionButLogIfTesPlanExecutionStartedListenerMethodFails(
102
91
@ Test
103
92
void shouldNotThrowExceptionButLogIfTesPlanExecutionFinishedListenerMethodFails (
104
93
LogRecordListener logRecordListener ) {
105
- var testDescriptor = getDemoMethodTestDescriptor ();
106
-
107
- compositeTestExecutionListener ().testPlanExecutionFinished (
108
- TestPlan .from (Set .of (testDescriptor ), mock (ConfigurationParameters .class )));
94
+ compositeTestExecutionListener ().testPlanExecutionFinished (anyTestPlan ());
109
95
110
96
assertThatTestListenerErrorLogged (logRecordListener , ThrowingTestExecutionListener .class ,
111
97
"testPlanExecutionFinished" );
@@ -116,8 +102,7 @@ void shouldNotThrowExceptionButLogIfExecutionJustStartedEagerTestListenerMethodF
116
102
LogRecordListener logRecordListener ) {
117
103
listeners .add (new ThrowingEagerTestExecutionListener ());
118
104
119
- var testIdentifier = getSampleMethodTestIdentifier ();
120
- compositeTestExecutionListener ().executionStarted (testIdentifier );
105
+ compositeTestExecutionListener ().executionStarted (anyTestIdentifier ());
121
106
122
107
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEagerTestExecutionListener .class ,
123
108
"executionJustStarted" );
@@ -128,8 +113,7 @@ void shouldNotThrowExceptionButLogIfExecutionJustFinishedEagerTestListenerMethod
128
113
LogRecordListener logRecordListener ) {
129
114
listeners .add (new ThrowingEagerTestExecutionListener ());
130
115
131
- var testIdentifier = getSampleMethodTestIdentifier ();
132
- compositeTestExecutionListener ().executionFinished (testIdentifier , mock (TestExecutionResult .class ));
116
+ compositeTestExecutionListener ().executionFinished (anyTestIdentifier (), anyTestExecutionResult ());
133
117
134
118
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEagerTestExecutionListener .class ,
135
119
"executionJustFinished" );
@@ -144,8 +128,8 @@ public void executionStarted(TestIdentifier testIdentifier) {
144
128
throw new OutOfMemoryError ();
145
129
}
146
130
});
147
- var testIdentifier = getSampleMethodTestIdentifier ();
148
- assertThatThrownBy (() -> compositeTestExecutionListener ().executionStarted (testIdentifier )).isInstanceOf (
131
+
132
+ assertThatThrownBy (() -> compositeTestExecutionListener ().executionStarted (anyTestIdentifier () )).isInstanceOf (
149
133
OutOfMemoryError .class );
150
134
151
135
assertNotLogs (logRecordListener );
@@ -159,13 +143,42 @@ public void executionJustStarted(TestIdentifier testIdentifier) {
159
143
throw new OutOfMemoryError ();
160
144
}
161
145
});
162
- var testIdentifier = getSampleMethodTestIdentifier ();
163
- assertThatThrownBy (() -> compositeTestExecutionListener ().executionStarted (testIdentifier )).isInstanceOf (
146
+
147
+ assertThatThrownBy (() -> compositeTestExecutionListener ().executionStarted (anyTestIdentifier () )).isInstanceOf (
164
148
OutOfMemoryError .class );
165
149
166
150
assertNotLogs (logRecordListener );
167
151
}
168
152
153
+ @ Test
154
+ void callsListenersInReverseOrderForFinishedEvents () {
155
+ listeners .clear ();
156
+ var firstListener = mock (TestExecutionListener .class , "firstListener" );
157
+ var secondListener = mock (TestExecutionListener .class , "secondListener" );
158
+ listeners .add (firstListener );
159
+ listeners .add (secondListener );
160
+
161
+ var testPlan = anyTestPlan ();
162
+ var testIdentifier = anyTestIdentifier ();
163
+ var testExecutionResult = anyTestExecutionResult ();
164
+
165
+ var composite = compositeTestExecutionListener ();
166
+ composite .testPlanExecutionStarted (testPlan );
167
+ composite .executionStarted (testIdentifier );
168
+ composite .executionFinished (testIdentifier , testExecutionResult );
169
+ composite .testPlanExecutionFinished (testPlan );
170
+
171
+ InOrder inOrder = inOrder (firstListener , secondListener );
172
+ inOrder .verify (firstListener ).testPlanExecutionStarted (testPlan );
173
+ inOrder .verify (secondListener ).testPlanExecutionStarted (testPlan );
174
+ inOrder .verify (firstListener ).executionStarted (testIdentifier );
175
+ inOrder .verify (secondListener ).executionStarted (testIdentifier );
176
+ inOrder .verify (secondListener ).executionFinished (testIdentifier , testExecutionResult );
177
+ inOrder .verify (firstListener ).executionFinished (testIdentifier , testExecutionResult );
178
+ inOrder .verify (secondListener ).testPlanExecutionFinished (testPlan );
179
+ inOrder .verify (firstListener ).testPlanExecutionFinished (testPlan );
180
+ }
181
+
169
182
private TestExecutionListener compositeTestExecutionListener () {
170
183
return new CompositeTestExecutionListener (listeners );
171
184
}
@@ -179,9 +192,12 @@ private void assertNotLogs(LogRecordListener logRecordListener) throws Assertion
179
192
assertThat (logRecordListener .stream (CompositeTestExecutionListener .class , Level .WARNING ).count ()).isZero ();
180
193
}
181
194
182
- private TestIdentifier getSampleMethodTestIdentifier () {
183
- var demoMethodTestDescriptor = getDemoMethodTestDescriptor ();
184
- return TestIdentifier .from (demoMethodTestDescriptor );
195
+ private static TestExecutionResult anyTestExecutionResult () {
196
+ return TestExecutionResult .successful ();
197
+ }
198
+
199
+ private static TestIdentifier anyTestIdentifier () {
200
+ return TestIdentifier .from (anyTestDescriptor ());
185
201
}
186
202
187
203
private void assertThatTestListenerErrorLogged (LogRecordListener logRecordListener , Class <?> listenerClass ,
@@ -190,10 +206,14 @@ private void assertThatTestListenerErrorLogged(LogRecordListener logRecordListen
190
206
"TestExecutionListener [" + listenerClass .getName () + "] threw exception for method: " + methodName );
191
207
}
192
208
193
- private DemoMethodTestDescriptor getDemoMethodTestDescriptor () {
194
- var method = ReflectionUtils .findMethod (this .getClass (), "getDemoMethodTestDescriptor" ,
195
- new Class <?>[0 ]).orElseThrow ();
196
- return new DemoMethodTestDescriptor (UniqueId .root ("method" , "unique_id" ), this .getClass (), method );
209
+ private static TestPlan anyTestPlan () {
210
+ return TestPlan .from (Set .of (anyTestDescriptor ()), mock (ConfigurationParameters .class ));
211
+ }
212
+
213
+ private static DemoMethodTestDescriptor anyTestDescriptor () {
214
+ var testClass = CompositeTestExecutionListenerTests .class ;
215
+ var method = ReflectionUtils .findMethod (testClass , "anyTestDescriptor" , new Class <?>[0 ]).orElseThrow ();
216
+ return new DemoMethodTestDescriptor (UniqueId .root ("method" , "unique_id" ), testClass , method );
197
217
}
198
218
199
219
private static class ThrowingEagerTestExecutionListener extends ThrowingTestExecutionListener
0 commit comments