Search code examples
intellij-ideacucumber-javacucumber-junit

JUnit + Cucumber in IntelliJ - See Steps


I have a Cucumber project that is configured to run with Junit by configuring my Junit test with

@RunWith(Cucumber.class)
@CucumberOptions(
        extraGlue = "me.jkstrauss.stepdefs.common",
        plugin = {"pretty", "html:target/cucumber/cucumber.html"}
)

I am trying to run the test in IntelliJ, and everything works. The problem is that when I view the results, I can only see the individual scenarios, but not the steps.

IntelliJ w/ JUnit

Of course, I can run the features directly as a Cucumber Java test in IntelliJ, and I get the expected output.

IntelliJ w/ Cucumber Java

The problem is that I want to use some JUnit features, such as ClassRule, and those will not be run as part of the dedicated Cucumber Java test. Is there any way that I can get the itemized steps, and at the same time retain all the JUnit functionality?


Solution

  • After some research on this topic I figured out that most of the JUnit plugin functionality can be replicated in Cucumber with a Cucumber plugin, so I am basically going to run all tests with the Cucumber plugin. I will still use Junit to run my tests from the command line, where the output goes to the screen either way, but when running Cucumber tests in IntelliJ I can just use the Cucumber plugin to accomplish whatever I wanted to do with Junit plugins.

    @RunWith(Cucumber.class)
    @CucumberOptions(
        plugin = {"me.jkstrauss.HealthRunner$Resources"}
    )
    class HealthRunner {
    
        public static class Resources implements EventListener {
    
            @Override
            public void setEventPublisher(EventPublisher publisher) {
                publisher.registerHandlerFor(TestRunFinished.class, it -> {
                    // remove unsued resources
            });
        }
    }
    

    To activate the plugin in Cucumber Run configuration just add --plugin me.jkstrauss.HealthRunner$Resources to the "Program arguments".

    Cucumber has many other events besides TestRunFinished. Look up API docs of https://javadoc.io/static/io.cucumber/cucumber-plugin/6.10.4/io/cucumber/plugin/event/EventPublisher.html for other options.