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 ;
29
30
import org .junit .platform .engine .UniqueId ;
30
31
import org .junit .platform .engine .reporting .ReportEntry ;
31
32
import org .junit .platform .engine .support .descriptor .DemoMethodTestDescriptor ;
33
+ import org .mockito .InOrder ;
32
34
33
35
@ TrackLogRecords
34
36
class CompositeEngineExecutionListenerTests {
@@ -38,37 +40,29 @@ class CompositeEngineExecutionListenerTests {
38
40
39
41
@ Test
40
42
void shouldNotThrowExceptionButLogIfDynamicTestRegisteredListenerMethodFails (LogRecordListener logRecordListener ) {
41
- var testDescriptor = getSampleMethodTestDescriptor ();
42
-
43
- compositeTestExecutionListener ().dynamicTestRegistered (testDescriptor );
43
+ compositeEngineExecutionListener ().dynamicTestRegistered (anyTestDescriptor ());
44
44
45
45
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEngineExecutionListener .class ,
46
46
"dynamicTestRegistered" );
47
47
}
48
48
49
49
@ Test
50
50
void shouldNotThrowExceptionButLogIfExecutionStartedListenerMethodFails (LogRecordListener logRecordListener ) {
51
- var testDescriptor = getSampleMethodTestDescriptor ();
52
-
53
- compositeTestExecutionListener ().executionStarted (testDescriptor );
51
+ compositeEngineExecutionListener ().executionStarted (anyTestDescriptor ());
54
52
55
53
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEngineExecutionListener .class , "executionStarted" );
56
54
}
57
55
58
56
@ Test
59
57
void shouldNotThrowExceptionButLogIfExecutionSkippedListenerMethodFails (LogRecordListener logRecordListener ) {
60
- var testDescriptor = getSampleMethodTestDescriptor ();
61
-
62
- compositeTestExecutionListener ().executionSkipped (testDescriptor , "deliberately skipped container" );
58
+ compositeEngineExecutionListener ().executionSkipped (anyTestDescriptor (), "deliberately skipped container" );
63
59
64
60
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEngineExecutionListener .class , "executionSkipped" );
65
61
}
66
62
67
63
@ Test
68
64
void shouldNotThrowExceptionButLogIfExecutionFinishedListenerMethodFails (LogRecordListener logRecordListener ) {
69
- var testDescriptor = getSampleMethodTestDescriptor ();
70
-
71
- compositeTestExecutionListener ().executionFinished (testDescriptor , mock (TestExecutionResult .class ));
65
+ compositeEngineExecutionListener ().executionFinished (anyTestDescriptor (), anyTestExecutionResult ());
72
66
73
67
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEngineExecutionListener .class ,
74
68
"executionFinished" );
@@ -77,9 +71,7 @@ void shouldNotThrowExceptionButLogIfExecutionFinishedListenerMethodFails(LogReco
77
71
@ Test
78
72
void shouldNotThrowExceptionButLogIfReportingEntryPublishedListenerMethodFails (
79
73
LogRecordListener logRecordListener ) {
80
- var testDescriptor = getSampleMethodTestDescriptor ();
81
-
82
- compositeTestExecutionListener ().reportingEntryPublished (testDescriptor , ReportEntry .from ("one" , "two" ));
74
+ compositeEngineExecutionListener ().reportingEntryPublished (anyTestDescriptor (), ReportEntry .from ("one" , "two" ));
83
75
84
76
assertThatTestListenerErrorLogged (logRecordListener , ThrowingEngineExecutionListener .class ,
85
77
"reportingEntryPublished" );
@@ -94,14 +86,37 @@ public void executionStarted(TestDescriptor testDescriptor) {
94
86
throw new OutOfMemoryError ();
95
87
}
96
88
});
97
- var testDescriptor = getSampleMethodTestDescriptor ();
98
- assertThatThrownBy (() -> compositeTestExecutionListener ().executionStarted (testDescriptor )).isInstanceOf (
89
+ var testDescriptor = anyTestDescriptor ();
90
+
91
+ assertThatThrownBy (() -> compositeEngineExecutionListener ().executionStarted (testDescriptor )).isInstanceOf (
99
92
OutOfMemoryError .class );
100
93
101
94
assertNotLogs (logRecordListener );
102
95
}
103
96
104
- private EngineExecutionListener compositeTestExecutionListener () {
97
+ @ Test
98
+ void callsListenersInReverseOrderForFinishedEvents () {
99
+ listeners .clear ();
100
+ var firstListener = mock (EngineExecutionListener .class , "firstListener" );
101
+ var secondListener = mock (EngineExecutionListener .class , "secondListener" );
102
+ listeners .add (firstListener );
103
+ listeners .add (secondListener );
104
+
105
+ var testDescriptor = anyTestDescriptor ();
106
+ var testExecutionResult = anyTestExecutionResult ();
107
+
108
+ var composite = compositeEngineExecutionListener ();
109
+ composite .executionStarted (testDescriptor );
110
+ composite .executionFinished (testDescriptor , testExecutionResult );
111
+
112
+ InOrder inOrder = inOrder (firstListener , secondListener );
113
+ inOrder .verify (firstListener ).executionStarted (testDescriptor );
114
+ inOrder .verify (secondListener ).executionStarted (testDescriptor );
115
+ inOrder .verify (secondListener ).executionFinished (testDescriptor , testExecutionResult );
116
+ inOrder .verify (firstListener ).executionFinished (testDescriptor , testExecutionResult );
117
+ }
118
+
119
+ private EngineExecutionListener compositeEngineExecutionListener () {
105
120
return new CompositeEngineExecutionListener (listeners );
106
121
}
107
122
@@ -114,8 +129,8 @@ private void assertNotLogs(LogRecordListener logRecordListener) throws Assertion
114
129
assertThat (logRecordListener .stream (CompositeEngineExecutionListener .class , Level .WARNING ).count ()).isZero ();
115
130
}
116
131
117
- private TestDescriptor getSampleMethodTestDescriptor () {
118
- return getDemoMethodTestDescriptor ( );
132
+ private static TestExecutionResult anyTestExecutionResult () {
133
+ return mock ( TestExecutionResult . class );
119
134
}
120
135
121
136
private void assertThatTestListenerErrorLogged (LogRecordListener logRecordListener , Class <?> listenerClass ,
@@ -124,10 +139,10 @@ private void assertThatTestListenerErrorLogged(LogRecordListener logRecordListen
124
139
"EngineExecutionListener [" + listenerClass .getName () + "] threw exception for method: " + methodName );
125
140
}
126
141
127
- private DemoMethodTestDescriptor getDemoMethodTestDescriptor () {
128
- var method = ReflectionUtils . findMethod ( this . getClass (), "getDemoMethodTestDescriptor" ,
129
- new Class <?>[0 ]).orElseThrow ();
130
- return new DemoMethodTestDescriptor (UniqueId .root ("method" , "unique_id" ), this . getClass () , method );
142
+ private static TestDescriptor anyTestDescriptor () {
143
+ var testClass = CompositeEngineExecutionListenerTests . class ;
144
+ var method = ReflectionUtils . findMethod ( testClass , "anyTestDescriptor" , new Class <?>[0 ]).orElseThrow ();
145
+ return new DemoMethodTestDescriptor (UniqueId .root ("method" , "unique_id" ), testClass , method );
131
146
}
132
147
133
148
private static class ThrowingEngineExecutionListener implements EngineExecutionListener {
0 commit comments