Skip to content

Commit

Permalink
[Core] Emit exceptions on failure to handle test run finished events
Browse files Browse the repository at this point in the history
The runtime would collect exceptions thrown upon emitting the TestRunFinished
event. The assumption was that these were collected in the test context.
Though this is impossible. The test context is collecting exceptions to include
in the report. So it sits inside the test run started and finished events.
  • Loading branch information
mpkorstanje committed Dec 2, 2022
1 parent d9a5afd commit e76ad72
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Enabled reproducible builds ([2641](https://github.com/cucumber/cucumber-jvm/issues/2641) Hervé Boutemy )
- [Core] Mark Allure 5 and 6 plugins as incompatible ([2652](https://github.com/cucumber/cucumber-jvm/issues/2652) M.P. Korstanje)

## Fixed
- [Core] Emit exceptions on failure to handle test run finished events ([2651](https://github.com/cucumber/cucumber-jvm/issues/2651) M.P. Korstanje)

## [7.9.0] - 2022-11-01
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ public void run() {
context.runBeforeAllHooks();
runFeatures(features);
});
execute(context::runAfterAllHooks);
execute(context::finishTestRun);
try {
execute(context::runAfterAllHooks);
} finally {
context.finishTestRun();
}
Throwable exception = context.getThrowable();
if (exception != null) {
throwAsUncheckedException(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import io.cucumber.core.backend.Pending;

import java.io.PrintStream;
import java.io.PrintWriter;

@Pending
public final class StubPendingException extends RuntimeException {

Expand All @@ -13,4 +16,14 @@ public StubPendingException(String message) {
super(message);
}

@Override
public void printStackTrace(PrintWriter printWriter) {
printWriter.print(getMessage());
}

@Override
public void printStackTrace(PrintStream printStream) {
printStream.print(getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.TestStepStarted;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -366,6 +367,40 @@ void should_fail_on_event_listener_exception_when_running_in_parallel() {
assertThat(actualThrown.getSuppressed(), is(arrayWithSize(3)));
}

@Test
void should_fail_on_event_listener_exception_at_test_run_started() {
RuntimeException expectedException = new RuntimeException("This exception is expected");
ConcurrentEventListener brokenEventListener = publisher -> publisher.registerHandlerFor(TestRunStarted.class,
(TestRunStarted event) -> {
throw expectedException;
});

Executable testMethod = () -> Runtime.builder()
.withFeatureSupplier(new StubFeatureSupplier())
.withAdditionalPlugins(brokenEventListener)
.build()
.run();
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, equalTo(expectedException));
}

@Test
void should_fail_on_event_listener_exception_at_test_run_finished() {
RuntimeException expectedException = new RuntimeException("This exception is expected");
ConcurrentEventListener brokenEventListener = publisher -> publisher.registerHandlerFor(TestRunFinished.class,
(TestRunFinished event) -> {
throw expectedException;
});

Executable testMethod = () -> Runtime.builder()
.withFeatureSupplier(new StubFeatureSupplier())
.withAdditionalPlugins(brokenEventListener)
.build()
.run();
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, equalTo(expectedException));
}

@Test
void should_interrupt_waiting_plugins() throws InterruptedException {
final Feature feature1 = TestFeatureParser.parse("path/test.feature", "" +
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<groupId>io.cucumber</groupId>
<artifactId>cucumber-parent</artifactId>
<version>4.1.1</version>
<relativePath/>
</parent>
<artifactId>cucumber-jvm</artifactId>
<version>7.10.0-SNAPSHOT</version>
Expand Down

0 comments on commit e76ad72

Please sign in to comment.