cucumber-jvm
cucumber-jvm copied to clipboard
Exceptions are swallowed (or not) depending on the `Envelope` content
👓 What did you see?
Let's say I have a plugin which process Envelope
and may generate exceptions. Depending on the Envelope
content, the exceptions are rethrowed or swallowed by Cucumber:
public class MyPluginPlugin implements EventListener, Plugin {
private final EventHandler<Envelope> testParameterTypeHandler = (event) -> {
if (event.getParameterType().isPresent()) {
throw new RuntimeException("getParameterType");// will be swallowed
} else if (event.getStepDefinition().isPresent()) {
throw new RuntimeException("getStepDefinition");// will be swallowed
} else if (event.getGherkinDocument().isPresent()) {
throw new RuntimeException("getGherkinDocument");// will be swallowed
} else if (event.getTestCaseStarted().isPresent()) {
throw new RuntimeException("getTestCaseStarted");// will be swallowed
} else if (event.getTestRunFinished().isPresent()) {
//throw new RuntimeException("getTestRunFinished"); // not swallowed
}
};
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(Envelope.class, this.testParameterTypeHandler);
}
}
Note: this is not the real plugin, just a minimalistic one designed to show the problem.
✅ What did you expect to see?
I expect the exceptions generated by the plugin are all rethrown and not swallowed.
📦 Which tool/library version are you using?
Cucumber Java 7.12.0
🔬 How could we reproduce it?
Steps to reproduce the behavior:
- create a plugin with the code above
- add the plugin to the run configuration of any project which uses Cucumber
- run the test with the plugin
- no exception is raised
📚 Any additional context?
As a workaround, I currently keep the generated exception aside and throw it when the Envelope
contains a TestRunFinished
event:
public class MyPluginPlugin implements EventListener, Plugin {
RuntimeException runtimeException = null;
private final EventHandler<Envelope> testParameterTypeHandler = (event) -> {
if (event.getParameterType().isPresent()) {
runtimeException = new RuntimeException("getParameterType");// will be swallowed
} else if (event.getStepDefinition().isPresent()) {
runtimeException = new RuntimeException("getStepDefinition");// will be swallowed
} else if (event.getGherkinDocument().isPresent()) {
runtimeException = new RuntimeException("getGherkinDocument");// will be swallowed
} else if (event.getTestCaseStarted().isPresent()) {
runtimeException = new RuntimeException("getTestCaseStarted");// will be swallowed
} else if (event.getTestRunFinished().isPresent()) {
//throw new RuntimeException("getTestRunFinished"); // not swallowed
if (runtimeException != null) {
throw runtimeException;
}
}
};
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(Envelope.class, this.testParameterTypeHandler);
}
}