cucumber-jvm icon indicating copy to clipboard operation
cucumber-jvm copied to clipboard

Exceptions are swallowed (or not) depending on the `Envelope` content

Open jkronegg opened this issue 1 year ago • 0 comments

👓 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:

  1. create a plugin with the code above
  2. add the plugin to the run configuration of any project which uses Cucumber
  3. run the test with the plugin
  4. 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);
    }

}

jkronegg avatar May 10 '23 10:05 jkronegg