1
1
package cucumber .runtime .junit ;
2
2
3
- import cucumber .api .PendingException ;
4
3
import cucumber .api .Result ;
4
+ import cucumber .api .TestStep ;
5
5
import cucumber .api .event .EventHandler ;
6
+ import cucumber .api .event .TestCaseFinished ;
7
+ import cucumber .api .event .TestCaseStarted ;
6
8
import cucumber .api .event .TestStepFinished ;
7
9
import cucumber .api .event .TestStepStarted ;
8
10
import cucumber .runner .EventBus ;
13
15
import org .junit .runner .notification .RunNotifier ;
14
16
import org .junit .runners .model .MultipleFailureException ;
15
17
16
- import static cucumber . runtime . Runtime . isPending ;
18
+ import java . util . ArrayList ;
17
19
18
20
public class JUnitReporter {
19
21
@@ -24,8 +26,15 @@ public class JUnitReporter {
24
26
private PickleRunner pickleRunner ;
25
27
private RunNotifier runNotifier ;
26
28
TestNotifier pickleRunnerNotifier ; // package-private for testing
27
- private boolean failedStep ;
28
- private boolean ignoredStep ;
29
+ ArrayList <Throwable > stepErrors ; // package-private for testing
30
+ private final EventHandler <TestCaseStarted > testCaseStartedHandler = new EventHandler <TestCaseStarted >() {
31
+
32
+ @ Override
33
+ public void receive (TestCaseStarted event ) {
34
+ handleTestCaseStarted ();
35
+ }
36
+
37
+ };
29
38
private final EventHandler <TestStepStarted > testStepStartedHandler = new EventHandler <TestStepStarted >() {
30
39
31
40
@ Override
@@ -40,38 +49,43 @@ public void receive(TestStepStarted event) {
40
49
41
50
@ Override
42
51
public void receive (TestStepFinished event ) {
43
- if (event .testStep .isHook ()) {
44
- handleHookResult ( event .result );
52
+ if (! event .testStep .isHook ()) {
53
+ handleStepResult ( event . testStep , event .result );
45
54
} else {
46
- handleStepResult (event .result );
55
+ handleHookResult (event .result );
47
56
}
48
57
}
49
58
59
+ };
60
+ private final EventHandler <TestCaseFinished > testCaseFinishedHandler = new EventHandler <TestCaseFinished >() {
61
+
62
+ @ Override
63
+ public void receive (TestCaseFinished event ) {
64
+ handleTestCaseResult (event .result );
65
+ }
66
+
50
67
};
51
68
52
69
public JUnitReporter (EventBus bus , boolean strict , JUnitOptions junitOption ) {
53
70
this .strict = strict ;
54
71
this .junitOptions = junitOption ;
72
+ bus .registerHandlerFor (TestCaseStarted .class , testCaseStartedHandler );
55
73
bus .registerHandlerFor (TestStepStarted .class , testStepStartedHandler );
56
74
bus .registerHandlerFor (TestStepFinished .class , testStepFinishedHandler );
75
+ bus .registerHandlerFor (TestCaseFinished .class , testCaseFinishedHandler );
57
76
}
58
77
59
78
void startExecutionUnit (PickleRunner pickleRunner , RunNotifier runNotifier ) {
60
79
this .pickleRunner = pickleRunner ;
61
80
this .runNotifier = runNotifier ;
62
81
this .stepNotifier = null ;
63
- this .failedStep = false ;
64
- this .ignoredStep = false ;
65
82
66
83
pickleRunnerNotifier = new EachTestNotifier (runNotifier , pickleRunner .getDescription ());
67
- pickleRunnerNotifier .fireTestStarted ();
68
84
}
69
85
70
- void finishExecutionUnit () {
71
- if (ignoredStep && !failedStep ) {
72
- pickleRunnerNotifier .fireTestIgnored ();
73
- }
74
- pickleRunnerNotifier .fireTestFinished ();
86
+ void handleTestCaseStarted () {
87
+ pickleRunnerNotifier .fireTestStarted ();
88
+ stepErrors = new ArrayList <Throwable >();
75
89
}
76
90
77
91
void handleStepStarted (PickleStep step ) {
@@ -81,82 +95,88 @@ void handleStepStarted(PickleStep step) {
81
95
} else {
82
96
stepNotifier = new NoTestNotifier ();
83
97
}
84
- if (junitOptions .allowStartedIgnored ()) {
85
- stepNotifier .fireTestStarted ();
86
- }
98
+ stepNotifier .fireTestStarted ();
87
99
}
88
100
89
101
boolean stepNotifications () {
90
102
return junitOptions .stepNotifications ();
91
103
}
92
104
93
- void handleStepResult (Result result ) {
105
+ void handleStepResult (TestStep testStep , Result result ) {
94
106
Throwable error = result .getError ();
95
- if (result .is (Result .Type .SKIPPED )) {
96
- if (error != null ) {
97
- stepNotifier .addFailedAssumption (error );
98
- pickleRunnerNotifier .addFailedAssumption (error );
107
+ switch (result .getStatus ()) {
108
+ case PASSED :
109
+ // do nothing
110
+ break ;
111
+ case SKIPPED :
112
+ if (error == null ) {
113
+ error = new SkippedThrowable (NotificationLevel .STEP );
99
114
} else {
100
- stepNotifier .fireTestIgnored ();
101
- }
102
- } else if (isPendingOrUndefined (result )) {
103
- addFailureOrIgnoreStep (result );
104
- } else {
105
- if (stepNotifier != null ) {
106
- //Should only fireTestStarted if not ignored
107
- if (!junitOptions .allowStartedIgnored ()) {
108
- stepNotifier .fireTestStarted ();
109
- }
110
- if (error != null ) {
111
- stepNotifier .addFailure (error );
112
- }
113
- stepNotifier .fireTestFinished ();
115
+ stepErrors .add (error );
114
116
}
115
- if (error != null ) {
116
- failedStep = true ;
117
- pickleRunnerNotifier .addFailure (error );
117
+ stepNotifier .addFailedAssumption (error );
118
+ break ;
119
+ case PENDING :
120
+ stepErrors .add (error );
121
+ addFailureOrFailedAssumptionDependingOnStrictMode (stepNotifier , error );
122
+ break ;
123
+ case UNDEFINED :
124
+ if (error == null ) {
125
+ error = new UndefinedThrowable ();
118
126
}
119
- }
127
+ stepErrors .add (new UndefinedThrowable (testStep .getStepText ()));
128
+ addFailureOrFailedAssumptionDependingOnStrictMode (stepNotifier , error );
129
+ break ;
130
+ case FAILED :
131
+ stepErrors .add (error );
132
+ stepNotifier .addFailure (error );
133
+ }
134
+ stepNotifier .fireTestFinished ();
120
135
}
121
136
122
137
void handleHookResult (Result result ) {
123
- if (result .is (Result .Type .FAILED ) || (strict && isPending (result .getError ()))) {
124
- pickleRunnerNotifier .addFailure (result .getError ());
125
- } else if (isPending (result .getError ())) {
126
- ignoredStep = true ;
138
+ if (result .getError () != null ) {
139
+ stepErrors .add (result .getError ());
127
140
}
128
141
}
129
142
130
- boolean useFilenameCompatibleNames () {
131
- return junitOptions .filenameCompatibleNames ();
143
+ void handleTestCaseResult (Result result ) {
144
+ switch (result .getStatus ()) {
145
+ case PASSED :
146
+ // do nothing
147
+ break ;
148
+ case SKIPPED :
149
+ if (stepErrors .isEmpty ()) {
150
+ stepErrors .add (new SkippedThrowable (NotificationLevel .SCENARIO ));
151
+ }
152
+ for (Throwable error : stepErrors ) {
153
+ pickleRunnerNotifier .addFailedAssumption (error );
154
+ }
155
+ break ;
156
+ case PENDING :
157
+ case UNDEFINED :
158
+ for (Throwable error : stepErrors ) {
159
+ addFailureOrFailedAssumptionDependingOnStrictMode (pickleRunnerNotifier , error );
160
+ }
161
+ break ;
162
+ case FAILED :
163
+ for (Throwable error : stepErrors ) {
164
+ pickleRunnerNotifier .addFailure (error );
165
+ }
166
+ }
167
+ pickleRunnerNotifier .fireTestFinished ();
132
168
}
133
169
134
- private boolean isPendingOrUndefined (Result result ) {
135
- Throwable error = result .getError ();
136
- return result .is (Result .Type .UNDEFINED ) || isPending (error );
170
+ boolean useFilenameCompatibleNames () {
171
+ return junitOptions .filenameCompatibleNames ();
137
172
}
138
173
139
- private void addFailureOrIgnoreStep ( Result result ) {
174
+ private void addFailureOrFailedAssumptionDependingOnStrictMode ( TestNotifier notifier , Throwable error ) {
140
175
if (strict ) {
141
- if (!junitOptions .allowStartedIgnored ()) {
142
- stepNotifier .fireTestStarted ();
143
- }
144
- addFailure (result );
145
- stepNotifier .fireTestFinished ();
176
+ notifier .addFailure (error );
146
177
} else {
147
- ignoredStep = true ;
148
- stepNotifier .fireTestIgnored ();
149
- }
150
- }
151
-
152
- private void addFailure (Result result ) {
153
- Throwable error = result .getError ();
154
- if (error == null ) {
155
- error = new PendingException ();
178
+ notifier .addFailedAssumption (error );
156
179
}
157
- failedStep = true ;
158
- stepNotifier .addFailure (error );
159
- pickleRunnerNotifier .addFailure (error );
160
180
}
161
181
162
182
private interface TestNotifier {
@@ -167,13 +187,11 @@ private interface TestNotifier {
167
187
168
188
void addFailedAssumption (Throwable error );
169
189
170
- void fireTestIgnored ();
171
-
172
190
void fireTestFinished ();
173
191
}
174
192
175
193
176
- private static final class NoTestNotifier implements TestNotifier {
194
+ static final class NoTestNotifier implements TestNotifier {
177
195
178
196
@ Override
179
197
public void fireTestStarted () {
@@ -190,11 +208,6 @@ public void addFailedAssumption(Throwable error) {
190
208
// Does nothing
191
209
}
192
210
193
- @ Override
194
- public void fireTestIgnored () {
195
- // Does nothing
196
- }
197
-
198
211
@ Override
199
212
public void fireTestFinished () {
200
213
// Does nothing
@@ -236,9 +249,5 @@ public void fireTestFinished() {
236
249
public void fireTestStarted () {
237
250
notifier .fireTestStarted (description );
238
251
}
239
-
240
- public void fireTestIgnored () {
241
- notifier .fireTestIgnored (description );
242
- }
243
252
}
244
253
}
0 commit comments