Notes
Setup
Add the Cucumber BOM in dependency management, so all Cucumber modules share one version. Then cucumber java for the annotations, the cucumber JUnit platform engine so JUnit five can run features, cucumber picocontainer for sharing state, and the JUnit platform suite module for the runner. Feature files go under source, test, resources, features, so they are on the classpath.
<dependencyManagement> io.cucumber :cucumber-bom:7.20.1 (import) io.cucumber:cucumber-java io.cucumber:cucumber-junit- platform-engine io.cucumber:cucumber-picocontainer org.junit.platform: junit-platform-suite features: src/test/resources/ features/coupons.feature
The runner
The runner is an empty class with annotations. Suite and include engines cucumber tell JUnit to use the Cucumber engine. Select classpath resource features says where the feature files are. The glue property names the package with your step definitions, and the plugin property adds output: pretty prints steps to the console, and html writes a report. Maven runs it with mvn test, like any JUnit test.
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(
key = GLUE_PROPERTY_NAME,
value = "dev.algorung.steps")
@ConfigurationParameter(
key = PLUGIN_PROPERTY_NAME,
value = "pretty, html:target/
cucumber.html")
public class RunCucumberTest {} Step definitions
Step definitions are methods annotated with given, when or then and a Cucumber expression. Int and string become method parameters, converted for you. Keep steps thin: each one calls a helper, here a ShopRung client, so the logic is reusable outside Cucumber. Assertions use JUnit's assert equals. When you run a feature with a step nobody wrote yet, Cucumber prints a ready made snippet to paste in.
@Given("my cart has {int} {string}")
public void cartHas(int qty,
String name) {
shop.addToCart(name, qty);
}
@When("I apply the coupon {string}")
public void apply(String code) {
ctx.response = shop.coupon(code); }
@Then("the total is {string}")
public void total(String expected) {
assertEquals(expected, ...); } Sharing state
Steps in different classes need to share data: the login step stores a token, the coupon step needs it. Do not use static fields; they leak between scenarios and break parallel runs. With PicoContainer, declare a scenario context class and ask for it in each step class's constructor. Cucumber creates one context per scenario and gives the same instance to every step class in that scenario, then throws it away.
public class ScenarioContext {
String token;
Response response;
}
public class CouponSteps {
private final ScenarioContext ctx;
public CouponSteps(
ScenarioContext ctx) {
this.ctx = ctx; }
}
// one new instance per scenario Hooks
Hooks run around every scenario. A before hook resets ShopRung. Hooks can be limited by tag: before scenarios tagged at UI, start a browser; after them, if the scenario failed, attach a screenshot to the report with scenario dot attach, then quit the browser. API scenarios never pay the cost of starting a browser. Use hooks for setup and teardown only, never to hide test steps.
@Before
public void resetShop() {
shop.reset(); }
@Before("@ui")
public void startBrowser() { ... }
@After("@ui")
public void screenshot(Scenario s) {
if (s.isFailed()) s.attach(
screenshotBytes(), "image/png",
s.getName());
driver.quit(); } Data tables
A step can carry a table. Here the cart contains two notebooks and one field guide. In the step definition, the table arrives as a list of maps, one map per row, keyed by the header. Cucumber can also convert rows straight into your own objects with a data table type. Tables keep scenarios readable when one step needs several values.
Given my cart contains:
| product | qty |
| Clean Code Notebook | 2 |
| Testing Field Guide | 1 |
@Given("my cart contains:")
public void cart(
List<Map<String, String>> rows) {
rows.forEach(r -> shop.addToCart(
r.get("product"),
Integer.parseInt(r.get("qty")))); Parallel and reports
Cucumber runs scenarios in parallel through the JUnit platform: enable it in the properties file and choose a fixed parallelism, such as four. It only works if scenarios share nothing: no static state, a driver per scenario, and their own data. Report plugins write HTML, JSON and JUnit XML; teams often add Allure, which comes later in this part.
cucumber.execution.parallel .enabled = true cucumber.execution.parallel .config.strategy = fixed cucumber.execution.parallel .config.fixed.parallelism = 4 needs: no static state, own data per scenario reports: html, json, junit; Allure plugin (Part 7, ep. 5)
Common mistakes
Cucumber Java mistakes. Static fields to share data between steps. Fat step definitions full of logic instead of calls to helpers. One step class per feature, with the same step written five times. Patterns so loose that two definitions match one step, which Cucumber reports as ambiguous. Hooks doing real test work nobody can see in the scenario. And parallel runs on shared data.
- static fields to share data
- fat steps full of logic
- one step class per feature, duplicating steps
- ambiguous patterns that match two steps
- hooks doing test work
- parallel on shared data
Say it like this
In the interview: feature files live under test resources, and a JUnit platform suite runner sets the glue package and report plugins. Step definitions are thin and call API clients and page objects. State between steps goes through a context object injected by PicoContainer, never static fields. Tagged hooks reset data, start browsers only for UI scenarios and attach screenshots on failure. Tags select smoke runs, and scenarios run in parallel with isolated data.
- "Features in resources, a JUnit Platform suite runner
- with glue and plugins. Thin
- steps call API clients and page
- objects; state is shared via a PicoContainer context, never
- statics. Tagged hooks reset
- data and handle browsers and screenshots. Tags pick smoke
- runs; scenarios run parallel."