Provides lambda-based step definitions for Cucumber BDD testing framework with Java 8+ functional interfaces
npx @tessl/cli install tessl/maven-io-cucumber--cucumber-java8@7.22.00
# Cucumber Java8
1
2
Cucumber Java8 provides lambda-based step definitions for the Cucumber BDD (Behavior-Driven Development) testing framework. It enables developers to write test step definitions using lambda expressions and functional interfaces instead of traditional annotation-based approaches, making test code more concise and readable while leveraging Java 8+ functional programming features.
3
4
## Package Information
5
6
- **Package Name**: cucumber-java8
7
- **Package Type**: Maven
8
- **Group ID**: io.cucumber
9
- **Artifact ID**: cucumber-java8
10
- **Language**: Java (Java 8+)
11
- **Installation**: Add to `pom.xml`:
12
13
```xml
14
<dependency>
15
<groupId>io.cucumber</groupId>
16
<artifactId>cucumber-java8</artifactId>
17
<version>7.22.1</version>
18
<scope>test</scope>
19
</dependency>
20
```
21
22
## Core Imports
23
24
```java
25
import io.cucumber.java8.En;
26
```
27
28
For localized step definitions:
29
30
```java
31
import io.cucumber.java8.Fr; // French
32
import io.cucumber.java8.De; // German
33
// Other language interfaces available
34
```
35
36
For hooks and test context:
37
38
```java
39
import io.cucumber.java8.Scenario;
40
import io.cucumber.java8.Status;
41
```
42
43
## Basic Usage
44
45
```java
46
package com.example.stepdefs;
47
48
import io.cucumber.java8.En;
49
import io.cucumber.datatable.DataTable;
50
import io.cucumber.docstring.DocString;
51
52
import static org.junit.jupiter.api.Assertions.assertEquals;
53
54
public class CalculatorSteps implements En {
55
private Calculator calculator;
56
57
public CalculatorSteps() {
58
// Step definitions
59
Given("a calculator I just turned on", () -> {
60
calculator = new Calculator();
61
});
62
63
When("I add {int} and {int}", (Integer arg1, Integer arg2) -> {
64
calculator.push(arg1);
65
calculator.push(arg2);
66
calculator.push("+");
67
});
68
69
Then("the result is {double}", (Double expected) -> {
70
assertEquals(expected, calculator.value());
71
});
72
73
// Hooks
74
Before((Scenario scenario) -> {
75
System.out.println("Starting scenario: " + scenario.getName());
76
});
77
78
After((Scenario scenario) -> {
79
if (scenario.isFailed()) {
80
scenario.attach("Screenshot".getBytes(), "text/plain", "Debug info");
81
}
82
});
83
84
// Parameter type
85
ParameterType("amount", "(\\d+\\.\\d+)\\s([a-zA-Z]+)", (String[] values) ->
86
new Amount(new BigDecimal(values[0]), Currency.getInstance(values[1])));
87
88
// Data table type
89
DataTableType((Map<String, String> entry) -> new User(
90
entry.get("name"),
91
entry.get("email"),
92
Integer.parseInt(entry.get("age"))
93
));
94
}
95
}
96
```
97
98
## Architecture
99
100
Cucumber Java8 is built around functional interfaces and lambda expressions, providing:
101
102
- **Lambda-based Step Definitions**: Replace traditional annotations with lambda expressions for cleaner, more maintainable test code
103
- **Functional Interface Design**: Extensive use of functional interfaces (A0-A9) supporting 0-9 parameters for maximum flexibility
104
- **Internationalization Support**: Generated language interfaces for writing tests in native languages
105
- **Hook System**: Comprehensive lifecycle hooks for test setup, teardown, and step-level operations
106
- **Type System Integration**: Full support for Java generics and type safety throughout the API
107
- **Transformation Pipeline**: Rich set of transformers for converting between string representations and Java objects
108
109
## Capabilities
110
111
### Step Definitions
112
113
Core functionality for defining BDD test steps using lambda expressions. Supports multiple languages and parameter arities (0-9 parameters) with full type safety.
114
115
```java { .api }
116
public interface En extends LambdaGlue {
117
// Given, When, Then, And, But methods with 0-9 parameter support
118
default void Given(String expression, StepDefinitionBody.A0 body) { ... }
119
default void Given(String expression, StepDefinitionBody.A1<T1> body) { ... }
120
// ... up to A9 for 9 parameters
121
122
default void When(String expression, StepDefinitionBody.A0 body) { ... }
123
default void Then(String expression, StepDefinitionBody.A0 body) { ... }
124
default void And(String expression, StepDefinitionBody.A0 body) { ... }
125
default void But(String expression, StepDefinitionBody.A0 body) { ... }
126
}
127
```
128
129
[Step Definitions](./step-definitions.md)
130
131
### Test Lifecycle Hooks
132
133
Before and After hooks for scenario and step-level lifecycle management, with support for execution order and tag-based filtering.
134
135
```java { .api }
136
// Hook methods in LambdaGlue interface
137
default void Before(HookBody body) { ... }
138
default void Before(String tagExpression, HookBody body) { ... }
139
default void Before(int order, HookBody body) { ... }
140
default void Before(String tagExpression, int order, HookBody body) { ... }
141
142
default void After(HookBody body) { ... }
143
default void BeforeStep(HookBody body) { ... }
144
default void AfterStep(HookBody body) { ... }
145
146
// Functional interfaces for hook bodies
147
@FunctionalInterface
148
interface HookBody {
149
void accept(Scenario scenario) throws Throwable;
150
}
151
152
@FunctionalInterface
153
interface HookNoArgsBody {
154
void accept() throws Throwable;
155
}
156
```
157
158
[Hooks](./hooks.md)
159
160
### Data Transformation
161
162
Comprehensive transformation system for converting between string representations and Java objects, including parameter types, data table types, doc string types, and default transformers.
163
164
```java { .api }
165
// Parameter types with 1-9 parameter support
166
default <T> void ParameterType(String name, String regexp, ParameterDefinitionBody.A1<T> body) { ... }
167
168
// Data table transformers
169
default <T> void DataTableType(DataTableDefinitionBody<T> body) { ... }
170
default <T> void DataTableType(DataTableEntryDefinitionBody<T> body) { ... }
171
default <T> void DataTableType(DataTableRowDefinitionBody<T> body) { ... }
172
default <T> void DataTableType(DataTableCellDefinitionBody<T> body) { ... }
173
174
// Doc string transformers
175
default <T> void DocStringType(String contentType, DocStringDefinitionBody<T> body) { ... }
176
177
// Default transformers
178
default void DefaultParameterTransformer(DefaultParameterTransformerBody body) { ... }
179
default void DefaultDataTableCellTransformer(DefaultDataTableCellTransformerBody body) { ... }
180
default void DefaultDataTableEntryTransformer(DefaultDataTableEntryTransformerBody body) { ... }
181
```
182
183
[Transformers](./transformers.md)
184
185
### Test Context and Utilities
186
187
Test execution context access through the Scenario class, providing test metadata, status information, and reporting capabilities.
188
189
```java { .api }
190
public final class Scenario {
191
public Collection<String> getSourceTagNames() { ... }
192
public Status getStatus() { ... }
193
public boolean isFailed() { ... }
194
public void attach(byte[] data, String mediaType, String name) { ... }
195
public void attach(String data, String mediaType, String name) { ... }
196
public void log(String text) { ... }
197
public String getName() { ... }
198
public String getId() { ... }
199
public URI getUri() { ... }
200
public Integer getLine() { ... }
201
}
202
203
public enum Status {
204
PASSED, SKIPPED, PENDING, UNDEFINED, AMBIGUOUS, FAILED, UNUSED
205
}
206
207
public final class PendingException extends RuntimeException {
208
public PendingException() { ... }
209
public PendingException(String message) { ... }
210
}
211
```
212
213
[Test Context](./test-context.md)
214
215
## Types
216
217
```java { .api }
218
// Core functional interfaces for step definitions
219
interface StepDefinitionBody {
220
@FunctionalInterface
221
interface A0 {
222
void accept() throws Throwable;
223
}
224
225
@FunctionalInterface
226
interface A1<T1> {
227
void accept(T1 arg1) throws Throwable;
228
}
229
230
@FunctionalInterface
231
interface A2<T1, T2> {
232
void accept(T1 arg1, T2 arg2) throws Throwable;
233
}
234
235
// ... A3 through A9 with similar patterns for up to 9 parameters
236
}
237
238
// Parameter definition functional interfaces
239
interface ParameterDefinitionBody {
240
@FunctionalInterface
241
interface A1<R> {
242
R accept(String arg1) throws Throwable;
243
}
244
245
// ... A2 through A9 similar to StepDefinitionBody
246
}
247
248
// Data transformation functional interfaces
249
@FunctionalInterface
250
interface DataTableDefinitionBody<T> {
251
T accept(DataTable table) throws Throwable;
252
}
253
254
@FunctionalInterface
255
interface DataTableEntryDefinitionBody<T> {
256
T accept(Map<String, String> entry) throws Throwable;
257
}
258
259
@FunctionalInterface
260
interface DataTableRowDefinitionBody<T> {
261
T accept(List<String> row) throws Throwable;
262
}
263
264
@FunctionalInterface
265
interface DataTableCellDefinitionBody<T> {
266
T accept(String cell) throws Throwable;
267
}
268
269
@FunctionalInterface
270
interface DocStringDefinitionBody<T> {
271
T accept(String docString) throws Throwable;
272
}
273
274
@FunctionalInterface
275
interface DefaultParameterTransformerBody {
276
Object accept(String fromValue, Type toValueType) throws Throwable;
277
}
278
279
@FunctionalInterface
280
interface DefaultDataTableCellTransformerBody {
281
Object accept(String fromValue, Type toValueType) throws Throwable;
282
}
283
284
@FunctionalInterface
285
interface DefaultDataTableEntryTransformerBody {
286
Object accept(Map<String, String> fromValue, Type toValueType) throws Throwable;
287
}
288
```