在IntelliJ中使用JUnit+Cucumber时,如何在保留JUnit功能的同时查看详细测试步骤
Great question—this is a common pain point when combining Cucumber with JUnit in IntelliJ. Let’s break down a few solid solutions that let you keep JUnit features like ClassRule while seeing detailed step results in the test runner:
The root issue here is that when running via JUnit, IntelliJ’s test runner doesn’t parse the pretty plugin’s console output to display steps. Instead, you need to add plugins that generate JUnit-compatible reports, which IntelliJ can natively interpret to show individual steps.
Update your @CucumberOptions to include these plugins:
@RunWith(Cucumber.class) @CucumberOptions( extraGlue = "me.jkstrauss.stepdefs.common", plugin = { "pretty", // Keeps human-readable console output "html:target/cucumber/cucumber.html", // For standalone HTML report "junit:target/cucumber/cucumber.xml", // JUnit-style XML report "json:target/cucumber/cucumber.json" // JSON report (optional but helpful) } ) public class RunCucumberTests {}
After running your test class via JUnit:
- IntelliJ will automatically pick up the
cucumber.xmlfile and display each scenario’s steps in the test results panel (just expand the scenario node). - You’ll still have full access to all JUnit features like
ClassRuleandRule, since you’re executing through the JUnit runner.
If you’re open to updating your setup, Cucumber’s JUnit 5 integration offers better out-of-the-box step visibility in IntelliJ, while fully supporting JUnit’s modern extension model (which replaces ClassRule/Rule in JUnit 5).
Here’s how to set it up:
- Replace your
cucumber-junitdependency withcucumber-junit-platform-engine(verify compatible versions with your Cucumber core via official docs). - Swap the
@RunWith(Cucumber.class)annotation with JUnit 5’s@Cucumberannotation:@Cucumber( extraGlue = "me.jkstrauss.stepdefs.common", plugin = { "pretty", "html:target/cucumber/cucumber.html" } ) public class RunCucumberTests {} - For JUnit 5 equivalents of
ClassRule, use@RegisterExtensionwith a static extension field.
JUnit 5’s test runner in IntelliJ natively displays Cucumber steps without needing extra XML plugins, and you’ll retain all JUnit 5’s advanced features.
If you need full control over how steps are displayed, you can create a custom JUnit RunListener that hooks into Cucumber’s event system. This lets you log steps directly to the JUnit test results.
- Implement Cucumber’s
EventListenerto capture step start/finish events. - Attach this listener to your JUnit test class using
@CucumberOptions(plugin = {"your.package.CustomStepListener"}). - Inside the listener, use JUnit’s
RunListenermethods to report steps as child nodes of each scenario in the test results.
This requires more code, but it’s ideal if you need custom formatting or integration with other internal tools.
Key Notes
- Always ensure your Cucumber and JUnit versions are compatible (check Cucumber’s version matrix for guidance).
- When running your test class, make sure your IntelliJ run configuration is set to "JUnit" (not "Cucumber Java") to retain
ClassRuleand other JUnit-specific functionality.
内容的提问来源于stack exchange,提问作者Joseph K. Strauss

