-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
124 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...orm-tests/src/test/java/org/junit/platform/testkit/engine/ExecutionsIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2015-2018 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.testkit.engine; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Integration tests for {@link Executions}. | ||
* | ||
* @since 1.4 | ||
*/ | ||
class ExecutionsIntegrationTests { | ||
|
||
@Test | ||
void executionsFromSkippedTestEvents() { | ||
Events testEvents = getTestEvents(); | ||
|
||
// We expect 1 for both of the following cases, since an Execution can | ||
// be created for a "skipped event even if "started" and "finished" events | ||
// are filtered out. | ||
assertThat(testEvents.executions().skipped().count()).isEqualTo(1); | ||
assertThat(testEvents.skipped().executions().count()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void executionsFromSucceededTestEvents() { | ||
Events testEvents = getTestEvents(); | ||
|
||
// We expect 1 if the executions are created BEFORE filtering out "finished" events. | ||
assertThat(testEvents.executions().succeeded().count()).isEqualTo(1); | ||
// We expect 0 if the executions are created AFTER filtering out "finished" events. | ||
assertThat(testEvents.succeeded().executions().count()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void executionsFromAbortedTestEvents() { | ||
Events testEvents = getTestEvents(); | ||
|
||
// We expect 1 if the executions are created BEFORE filtering out "started" events. | ||
assertThat(testEvents.executions().aborted().count()).isEqualTo(1); | ||
// We expect 0 if the executions are created AFTER filtering out "started" events. | ||
assertThat(testEvents.aborted().executions().count()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void executionsFromFailedTestEvents() { | ||
Events testEvents = getTestEvents(); | ||
|
||
// We expect 1 if the executions are created BEFORE filtering out "started" events. | ||
assertThat(testEvents.executions().failed().count()).isEqualTo(1); | ||
// We expect 0 if the executions are created AFTER filtering out "started" events. | ||
assertThat(testEvents.failed().executions().count()).isEqualTo(0); | ||
} | ||
|
||
private Events getTestEvents() { | ||
Events testEvents = EngineTestKit.engine("junit-jupiter")// | ||
.selectors(selectClass(ExampleTestCase.class))// | ||
.execute()// | ||
.tests(); | ||
|
||
testEvents.assertStatistics(stats -> stats.skipped(1).started(3).succeeded(1).aborted(1).failed(1)); | ||
|
||
return testEvents; | ||
} | ||
|
||
static class ExampleTestCase { | ||
|
||
@Test | ||
@Disabled | ||
void skippedTest() { | ||
} | ||
|
||
@Test | ||
void succeedingTest() { | ||
} | ||
|
||
@Test | ||
void abortedTest() { | ||
assumeTrue(false); | ||
} | ||
|
||
@Test | ||
void failingTest() { | ||
fail("Boom!"); | ||
} | ||
|
||
} | ||
|
||
} |