Provides lambda-based step definitions for Cucumber BDD testing framework with Java 8+ functional interfaces
—
Lambda-based step definitions for Cucumber BDD tests using Java 8+ functional interfaces. Provides clean, readable alternatives to traditional annotation-based step definitions with full support for internationalization and multiple parameter arities.
Primary interface for English language step definitions, extending LambdaGlue with Given, When, Then, And, But methods.
/**
* English language step definitions interface providing lambda-based BDD step methods
* Extends LambdaGlue to inherit hook and transformer functionality
*/
public interface En extends LambdaGlue {
// Given step definitions (0-9 parameters)
default void Given(String expression, StepDefinitionBody.A0 body) { ... }
default void Given(String expression, StepDefinitionBody.A1<T1> body) { ... }
default void Given(String expression, StepDefinitionBody.A2<T1, T2> body) { ... }
default void Given(String expression, StepDefinitionBody.A3<T1, T2, T3> body) { ... }
default void Given(String expression, StepDefinitionBody.A4<T1, T2, T3, T4> body) { ... }
default void Given(String expression, StepDefinitionBody.A5<T1, T2, T3, T4, T5> body) { ... }
default void Given(String expression, StepDefinitionBody.A6<T1, T2, T3, T4, T5, T6> body) { ... }
default void Given(String expression, StepDefinitionBody.A7<T1, T2, T3, T4, T5, T6, T7> body) { ... }
default void Given(String expression, StepDefinitionBody.A8<T1, T2, T3, T4, T5, T6, T7, T8> body) { ... }
default void Given(String expression, StepDefinitionBody.A9<T1, T2, T3, T4, T5, T6, T7, T8, T9> body) { ... }
// When step definitions (0-9 parameters) - same pattern as Given
default void When(String expression, StepDefinitionBody.A0 body) { ... }
// ... When methods with A1 through A9
// Then step definitions (0-9 parameters) - same pattern as Given
default void Then(String expression, StepDefinitionBody.A0 body) { ... }
// ... Then methods with A1 through A9
// And step definitions (0-9 parameters) - same pattern as Given
default void And(String expression, StepDefinitionBody.A0 body) { ... }
// ... And methods with A1 through A9
// But step definitions (0-9 parameters) - same pattern as Given
default void But(String expression, StepDefinitionBody.A0 body) { ... }
// ... But methods with A1 through A9
}Usage Examples:
import io.cucumber.java8.En;
public class StepDefinitions implements En {
private Calculator calculator;
public StepDefinitions() {
// No parameters
Given("a calculator I just turned on", () -> {
calculator = new Calculator();
});
// Single parameter
When("I press {string}", (String button) -> {
calculator.press(button);
});
// Multiple parameters with type conversion
When("I add {int} and {int}", (Integer a, Integer b) -> {
calculator.add(a, b);
});
// Complex parameter extraction
Then("the result should be {double} with precision {int}", (Double expected, Integer precision) -> {
assertEquals(expected, calculator.getResult(), Math.pow(10, -precision));
});
// DataTable usage
Given("the following data:", (DataTable dataTable) -> {
List<Map<String, String>> data = dataTable.asMaps();
for (Map<String, String> row : data) {
calculator.addData(row.get("key"), row.get("value"));
}
});
// DocString usage
Then("the output should be:", (DocString docString) -> {
assertEquals(docString.getContent(), calculator.getOutput());
});
}
}Generated interfaces for different languages following the same pattern as En but with localized keywords.
/**
* French language step definitions interface
* Provides step methods using French Gherkin keywords: Soit, Quand, Alors, Et, Mais
*/
public interface Fr extends LambdaGlue {
// French equivalents: Soit (Given), Quand (When), Alors (Then), Et (And), Mais (But)
default void Soit(String expression, StepDefinitionBody.A0 body) { ... }
default void Quand(String expression, StepDefinitionBody.A0 body) { ... }
default void Alors(String expression, StepDefinitionBody.A0 body) { ... }
default void Et(String expression, StepDefinitionBody.A0 body) { ... }
default void Mais(String expression, StepDefinitionBody.A0 body) { ... }
// ... same A0-A9 parameter variations for each keyword
}
/**
* German language step definitions interface
* Provides step methods using German Gherkin keywords: Angenommen, Wenn, Dann, Und, Aber
*/
public interface De extends LambdaGlue {
// German equivalents: Angenommen (Given), Wenn (When), Dann (Then), Und (And), Aber (But)
default void Angenommen(String expression, StepDefinitionBody.A0 body) { ... }
default void Wenn(String expression, StepDefinitionBody.A0 body) { ... }
default void Dann(String expression, StepDefinitionBody.A0 body) { ... }
default void Und(String expression, StepDefinitionBody.A0 body) { ... }
default void Aber(String expression, StepDefinitionBody.A0 body) { ... }
// ... same A0-A9 parameter variations for each keyword
}Additional language interfaces are generated for all supported Gherkin languages including Spanish (Es), Italian (It), Portuguese (Pt), Russian (Ru), and many others.
Usage Example:
import io.cucumber.java8.Fr;
public class StepDefinitionsFrancais implements Fr {
private Calculatrice calculatrice;
public StepDefinitionsFrancais() {
Soit("une calculatrice que je viens d'allumer", () -> {
calculatrice = new Calculatrice();
});
Quand("j'additionne {int} et {int}", (Integer a, Integer b) -> {
calculatrice.additionner(a, b);
});
Alors("le résultat devrait être {double}", (Double attendu) -> {
assertEquals(attendu, calculatrice.getResultat());
});
}
}Functional interfaces defining lambda signatures for step definition implementations with support for 0-9 parameters.
/**
* Functional interfaces for step definition lambda bodies
* Supports 0-9 parameters with full type safety and exception propagation
*/
public interface StepDefinitionBody {
@FunctionalInterface
interface A0 {
/**
* Step definition with no parameters
* @throws Throwable Any exception during step execution
*/
void accept() throws Throwable;
}
@FunctionalInterface
interface A1<T1> {
/**
* Step definition with 1 parameter
* @param arg1 First extracted parameter
* @throws Throwable Any exception during step execution
*/
void accept(T1 arg1) throws Throwable;
}
@FunctionalInterface
interface A2<T1, T2> {
/**
* Step definition with 2 parameters
* @param arg1 First extracted parameter
* @param arg2 Second extracted parameter
* @throws Throwable Any exception during step execution
*/
void accept(T1 arg1, T2 arg2) throws Throwable;
}
@FunctionalInterface
interface A3<T1, T2, T3> {
void accept(T1 arg1, T2 arg2, T3 arg3) throws Throwable;
}
@FunctionalInterface
interface A4<T1, T2, T3, T4> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4) throws Throwable;
}
@FunctionalInterface
interface A5<T1, T2, T3, T4, T5> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) throws Throwable;
}
@FunctionalInterface
interface A6<T1, T2, T3, T4, T5, T6> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) throws Throwable;
}
@FunctionalInterface
interface A7<T1, T2, T3, T4, T5, T6, T7> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) throws Throwable;
}
@FunctionalInterface
interface A8<T1, T2, T3, T4, T5, T6, T7, T8> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) throws Throwable;
}
@FunctionalInterface
interface A9<T1, T2, T3, T4, T5, T6, T7, T8, T9> {
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) throws Throwable;
}
}Parameter Extraction:
Parameters are automatically extracted from step expressions using Cucumber's parameter matching:
// String parameter extraction
When("I enter {string}", (String text) -> { ... });
// Numeric parameter extraction
When("I wait {int} seconds", (Integer seconds) -> { ... });
When("I set the value to {double}", (Double value) -> { ... });
// Boolean parameter extraction
When("the flag is {boolean}", (Boolean flag) -> { ... });
// Custom parameter types (defined with ParameterType)
When("I transfer {amount} to account {int}", (Amount amount, Integer accountId) -> { ... });
// Mixed parameter types
When("user {string} has {int} items costing {double} each",
(String username, Integer count, Double unitPrice) -> { ... });All step definition bodies allow throwing any exception, which will be caught and handled by the Cucumber framework:
Given("a step that might fail", () -> {
if (someCondition) {
throw new RuntimeException("Step failed due to condition");
}
// Normal execution continues
});
// Use PendingException for unimplemented steps
Given("a step not yet implemented", () -> {
throw new PendingException("TODO: implement this step");
});Install with Tessl CLI
npx tessl i tessl/maven-io-cucumber--cucumber-java8