Skip to content

Commit f23a136

Browse files
committed
Avoid NullPointerException when creating Executions from filtered events
Prior to this commit, the logic in Executions.createExecutions() could result in a NullPointerException if the executions were being created from filtered events. For example, an attempt to create executions from `testEvents.failed().executions()` resulted in a NullPointerException since the "started" event (needed to determine the start Instant) was no longer present in the event list. This commit fixes this bug by avoiding the creation of Execution instances if the "started" event is absent. Consequently, an invocation such as `testEvents.failed().executions().count()` now returns `0` instead of throwing a NullPointerException. Issue: #1356
1 parent 9bb7786 commit f23a136

File tree

2 files changed

+124
-9
lines changed

2 files changed

+124
-9
lines changed

junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/Executions.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,32 @@ private static List<Execution> createExecutions(List<Event> events) {
261261
break;
262262
}
263263
case SKIPPED: {
264-
Instant startInstant = executionStarts.get(event.getTestDescriptor());
265-
Execution skippedEvent = Execution.skipped(event.getTestDescriptor(),
266-
startInstant != null ? startInstant : event.getTimestamp(), event.getTimestamp(),
264+
// Based on the Javadoc for EngineExecutionListener.executionSkipped(...),
265+
// a skipped descriptor must never be reported as started or finished,
266+
// but just in case a TestEngine does not adhere to that contract, we
267+
// make an attempt to remove any tracked "started" event.
268+
executionStarts.remove(event.getTestDescriptor());
269+
270+
// Use the same timestamp for both the start and end Instant values.
271+
Instant timestamp = event.getTimestamp();
272+
273+
Execution skippedEvent = Execution.skipped(event.getTestDescriptor(), timestamp, timestamp,
267274
event.getRequiredPayload(String.class));
268275
executions.add(skippedEvent);
269-
executionStarts.remove(event.getTestDescriptor());
270276
break;
271277
}
272278
case FINISHED: {
273-
Execution finishedEvent = Execution.finished(event.getTestDescriptor(),
274-
executionStarts.get(event.getTestDescriptor()), event.getTimestamp(),
275-
event.getRequiredPayload(TestExecutionResult.class));
276-
executions.add(finishedEvent);
277-
executionStarts.remove(event.getTestDescriptor());
279+
Instant startInstant = executionStarts.remove(event.getTestDescriptor());
280+
Instant endInstant = event.getTimestamp();
281+
282+
// If "started" events have already been filtered out, it is no
283+
// longer possible to create a legitimate Execution for the current
284+
// descriptor. So we effectively ignore it in that case.
285+
if (startInstant != null) {
286+
Execution finishedEvent = Execution.finished(event.getTestDescriptor(), startInstant,
287+
endInstant, event.getRequiredPayload(TestExecutionResult.class));
288+
executions.add(finishedEvent);
289+
}
278290
break;
279291
}
280292
default: {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2015-2018 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.testkit.engine;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.fail;
15+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
16+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
17+
18+
import org.junit.jupiter.api.Disabled;
19+
import org.junit.jupiter.api.Test;
20+
21+
/**
22+
* Integration tests for {@link Executions}.
23+
*
24+
* @since 1.4
25+
*/
26+
class ExecutionsIntegrationTests {
27+
28+
@Test
29+
void executionsFromSkippedTestEvents() {
30+
Events testEvents = getTestEvents();
31+
32+
// We expect 1 for both of the following cases, since an Execution can
33+
// be created for a "skipped event even if "started" and "finished" events
34+
// are filtered out.
35+
assertThat(testEvents.executions().skipped().count()).isEqualTo(1);
36+
assertThat(testEvents.skipped().executions().count()).isEqualTo(1);
37+
}
38+
39+
@Test
40+
void executionsFromSucceededTestEvents() {
41+
Events testEvents = getTestEvents();
42+
43+
// We expect 1 if the executions are created BEFORE filtering out "finished" events.
44+
assertThat(testEvents.executions().succeeded().count()).isEqualTo(1);
45+
// We expect 0 if the executions are created AFTER filtering out "finished" events.
46+
assertThat(testEvents.succeeded().executions().count()).isEqualTo(0);
47+
}
48+
49+
@Test
50+
void executionsFromAbortedTestEvents() {
51+
Events testEvents = getTestEvents();
52+
53+
// We expect 1 if the executions are created BEFORE filtering out "started" events.
54+
assertThat(testEvents.executions().aborted().count()).isEqualTo(1);
55+
// We expect 0 if the executions are created AFTER filtering out "started" events.
56+
assertThat(testEvents.aborted().executions().count()).isEqualTo(0);
57+
}
58+
59+
@Test
60+
void executionsFromFailedTestEvents() {
61+
Events testEvents = getTestEvents();
62+
63+
// We expect 1 if the executions are created BEFORE filtering out "started" events.
64+
assertThat(testEvents.executions().failed().count()).isEqualTo(1);
65+
// We expect 0 if the executions are created AFTER filtering out "started" events.
66+
assertThat(testEvents.failed().executions().count()).isEqualTo(0);
67+
}
68+
69+
private Events getTestEvents() {
70+
Events testEvents = EngineTestKit.engine("junit-jupiter")//
71+
.selectors(selectClass(ExampleTestCase.class))//
72+
.execute()//
73+
.tests();
74+
75+
testEvents.assertStatistics(stats -> stats.skipped(1).started(3).succeeded(1).aborted(1).failed(1));
76+
77+
return testEvents;
78+
}
79+
80+
static class ExampleTestCase {
81+
82+
@Test
83+
@Disabled
84+
void skippedTest() {
85+
}
86+
87+
@Test
88+
void succeedingTest() {
89+
}
90+
91+
@Test
92+
void abortedTest() {
93+
assumeTrue(false);
94+
}
95+
96+
@Test
97+
void failingTest() {
98+
fail("Boom!");
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)