Cucumber-JVM Java library providing annotation-based step definitions for Behavior-Driven Development (BDD) testing
npx @tessl/cli install tessl/maven-io-cucumber--cucumber-java@7.22.0Cucumber-Java provides annotation-based step definitions for Behavior-Driven Development (BDD) testing within the Cucumber framework. It enables developers to write automated tests in plain language that can be read by both technical and non-technical team members, improving communication and collaboration in software development teams.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.22.1</version>
<scope>test</scope>
</dependency>import io.cucumber.java.en.*;
import io.cucumber.java.Before;
import io.cucumber.java.After;
import io.cucumber.java.Scenario;
import io.cucumber.java.PendingException;
import io.cucumber.java.ParameterType;
import io.cucumber.java.DataTableType;
import io.cucumber.java.DocStringType;For other languages (internationalization):
import io.cucumber.java.fr.*; // French
import io.cucumber.java.de.*; // German
import io.cucumber.java.es.*; // Spanish
// 70+ other language packages availablepackage com.example.stepdefs;
import io.cucumber.java.en.*;
import io.cucumber.java.Before;
import io.cucumber.java.After;
import io.cucumber.java.Scenario;
public class CalculatorStepDefinitions {
private Calculator calc;
@Before
public void setUp() {
calc = new Calculator();
}
@Given("a calculator I just turned on")
public void a_calculator_I_just_turned_on() {
// Calculator already initialized in setUp
}
@When("I add {int} and {int}")
public void adding(int arg1, int arg2) {
calc.push(arg1);
calc.push(arg2);
calc.push("+");
}
@Then("the result is {int}")
public void the_result_is(int expected) {
assertEquals(expected, calc.value());
}
@After
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
scenario.log("Test failed: " + scenario.getName());
}
}
}Cucumber-Java is built around several key architectural components:
@Given, @When, @Then) generated for 70+ languages@Before, @After, @BeforeAll, @AfterAll, @BeforeStep, @AfterStep@ParameterType and default transformersScenario object provides access to test execution context and reportingCore step definition annotations for writing BDD test steps in multiple languages. Supports both Cucumber expressions and regular expressions.
// English annotations (most commonly used)
@Given("step text with {parameter}")
@When("step text with {parameter}")
@Then("step text with {parameter}")
@And("step text with {parameter}")
@But("step text with {parameter}")Comprehensive hook system for test setup, teardown, and step-level interception across different scopes.
@Before("@tag-expression")
public void setUp(Scenario scenario) { }
@After(order = 1000)
public void tearDown(Scenario scenario) { }
@BeforeAll // EXPERIMENTAL
public static void beforeAll() { }
@AfterAll // EXPERIMENTAL
public static void afterAll() { }Custom parameter types for converting step parameters from strings to domain objects, with extensive configuration options.
@ParameterType("\\d{4}-\\d{2}-\\d{2}")
public LocalDate date(String dateString) {
return LocalDate.parse(dateString);
}
@DefaultParameterTransformer
public Object defaultTransformer(String fromValue, Type toValueType) {
// Custom transformation logic
return transformed;
}Robust data table and doc string handling with custom transformations and built-in collection support.
@DataTableType
public Person personEntry(Map<String, String> entry) {
return new Person(entry.get("name"), entry.get("email"));
}
@DocStringType
public JsonNode json(String docString) {
return objectMapper.readTree(docString);
}Access to scenario information, status, and reporting capabilities during test execution.
public class Scenario {
Collection<String> getSourceTagNames();
Status getStatus();
boolean isFailed();
void attach(byte[] data, String mediaType, String name);
void attach(String data, String mediaType, String name);
void log(String text);
String getName();
String getId();
URI getUri();
Integer getLine();
}
public enum Status {
PASSED, SKIPPED, PENDING, UNDEFINED, AMBIGUOUS, FAILED, UNUSED
}// Core exception for marking unimplemented steps
public final class PendingException extends RuntimeException {
public PendingException();
public PendingException(String message);
}
// Annotation for transposing data tables (used on parameters)
public void stepWithTransposedTable(@Transpose DataTable table) { }
// Meta-annotations for step definitions (internal use)
@StepDefinitionAnnotation
@StepDefinitionAnnotations