0
# Step Definitions
1
2
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.
3
4
## Capabilities
5
6
### En Interface (English)
7
8
Primary interface for English language step definitions, extending LambdaGlue with Given, When, Then, And, But methods.
9
10
```java { .api }
11
/**
12
* English language step definitions interface providing lambda-based BDD step methods
13
* Extends LambdaGlue to inherit hook and transformer functionality
14
*/
15
public interface En extends LambdaGlue {
16
// Given step definitions (0-9 parameters)
17
default void Given(String expression, StepDefinitionBody.A0 body) { ... }
18
default void Given(String expression, StepDefinitionBody.A1<T1> body) { ... }
19
default void Given(String expression, StepDefinitionBody.A2<T1, T2> body) { ... }
20
default void Given(String expression, StepDefinitionBody.A3<T1, T2, T3> body) { ... }
21
default void Given(String expression, StepDefinitionBody.A4<T1, T2, T3, T4> body) { ... }
22
default void Given(String expression, StepDefinitionBody.A5<T1, T2, T3, T4, T5> body) { ... }
23
default void Given(String expression, StepDefinitionBody.A6<T1, T2, T3, T4, T5, T6> body) { ... }
24
default void Given(String expression, StepDefinitionBody.A7<T1, T2, T3, T4, T5, T6, T7> body) { ... }
25
default void Given(String expression, StepDefinitionBody.A8<T1, T2, T3, T4, T5, T6, T7, T8> body) { ... }
26
default void Given(String expression, StepDefinitionBody.A9<T1, T2, T3, T4, T5, T6, T7, T8, T9> body) { ... }
27
28
// When step definitions (0-9 parameters) - same pattern as Given
29
default void When(String expression, StepDefinitionBody.A0 body) { ... }
30
// ... When methods with A1 through A9
31
32
// Then step definitions (0-9 parameters) - same pattern as Given
33
default void Then(String expression, StepDefinitionBody.A0 body) { ... }
34
// ... Then methods with A1 through A9
35
36
// And step definitions (0-9 parameters) - same pattern as Given
37
default void And(String expression, StepDefinitionBody.A0 body) { ... }
38
// ... And methods with A1 through A9
39
40
// But step definitions (0-9 parameters) - same pattern as Given
41
default void But(String expression, StepDefinitionBody.A0 body) { ... }
42
// ... But methods with A1 through A9
43
}
44
```
45
46
**Usage Examples:**
47
48
```java
49
import io.cucumber.java8.En;
50
51
public class StepDefinitions implements En {
52
private Calculator calculator;
53
54
public StepDefinitions() {
55
// No parameters
56
Given("a calculator I just turned on", () -> {
57
calculator = new Calculator();
58
});
59
60
// Single parameter
61
When("I press {string}", (String button) -> {
62
calculator.press(button);
63
});
64
65
// Multiple parameters with type conversion
66
When("I add {int} and {int}", (Integer a, Integer b) -> {
67
calculator.add(a, b);
68
});
69
70
// Complex parameter extraction
71
Then("the result should be {double} with precision {int}", (Double expected, Integer precision) -> {
72
assertEquals(expected, calculator.getResult(), Math.pow(10, -precision));
73
});
74
75
// DataTable usage
76
Given("the following data:", (DataTable dataTable) -> {
77
List<Map<String, String>> data = dataTable.asMaps();
78
for (Map<String, String> row : data) {
79
calculator.addData(row.get("key"), row.get("value"));
80
}
81
});
82
83
// DocString usage
84
Then("the output should be:", (DocString docString) -> {
85
assertEquals(docString.getContent(), calculator.getOutput());
86
});
87
}
88
}
89
```
90
91
### Localized Language Interfaces
92
93
Generated interfaces for different languages following the same pattern as En but with localized keywords.
94
95
```java { .api }
96
/**
97
* French language step definitions interface
98
* Provides step methods using French Gherkin keywords: Soit, Quand, Alors, Et, Mais
99
*/
100
public interface Fr extends LambdaGlue {
101
// French equivalents: Soit (Given), Quand (When), Alors (Then), Et (And), Mais (But)
102
default void Soit(String expression, StepDefinitionBody.A0 body) { ... }
103
default void Quand(String expression, StepDefinitionBody.A0 body) { ... }
104
default void Alors(String expression, StepDefinitionBody.A0 body) { ... }
105
default void Et(String expression, StepDefinitionBody.A0 body) { ... }
106
default void Mais(String expression, StepDefinitionBody.A0 body) { ... }
107
// ... same A0-A9 parameter variations for each keyword
108
}
109
110
/**
111
* German language step definitions interface
112
* Provides step methods using German Gherkin keywords: Angenommen, Wenn, Dann, Und, Aber
113
*/
114
public interface De extends LambdaGlue {
115
// German equivalents: Angenommen (Given), Wenn (When), Dann (Then), Und (And), Aber (But)
116
default void Angenommen(String expression, StepDefinitionBody.A0 body) { ... }
117
default void Wenn(String expression, StepDefinitionBody.A0 body) { ... }
118
default void Dann(String expression, StepDefinitionBody.A0 body) { ... }
119
default void Und(String expression, StepDefinitionBody.A0 body) { ... }
120
default void Aber(String expression, StepDefinitionBody.A0 body) { ... }
121
// ... same A0-A9 parameter variations for each keyword
122
}
123
```
124
125
Additional language interfaces are generated for all supported Gherkin languages including Spanish (Es), Italian (It), Portuguese (Pt), Russian (Ru), and many others.
126
127
**Usage Example:**
128
129
```java
130
import io.cucumber.java8.Fr;
131
132
public class StepDefinitionsFrancais implements Fr {
133
private Calculatrice calculatrice;
134
135
public StepDefinitionsFrancais() {
136
Soit("une calculatrice que je viens d'allumer", () -> {
137
calculatrice = new Calculatrice();
138
});
139
140
Quand("j'additionne {int} et {int}", (Integer a, Integer b) -> {
141
calculatrice.additionner(a, b);
142
});
143
144
Alors("le résultat devrait être {double}", (Double attendu) -> {
145
assertEquals(attendu, calculatrice.getResultat());
146
});
147
}
148
}
149
```
150
151
### Step Definition Body Functional Interfaces
152
153
Functional interfaces defining lambda signatures for step definition implementations with support for 0-9 parameters.
154
155
```java { .api }
156
/**
157
* Functional interfaces for step definition lambda bodies
158
* Supports 0-9 parameters with full type safety and exception propagation
159
*/
160
public interface StepDefinitionBody {
161
@FunctionalInterface
162
interface A0 {
163
/**
164
* Step definition with no parameters
165
* @throws Throwable Any exception during step execution
166
*/
167
void accept() throws Throwable;
168
}
169
170
@FunctionalInterface
171
interface A1<T1> {
172
/**
173
* Step definition with 1 parameter
174
* @param arg1 First extracted parameter
175
* @throws Throwable Any exception during step execution
176
*/
177
void accept(T1 arg1) throws Throwable;
178
}
179
180
@FunctionalInterface
181
interface A2<T1, T2> {
182
/**
183
* Step definition with 2 parameters
184
* @param arg1 First extracted parameter
185
* @param arg2 Second extracted parameter
186
* @throws Throwable Any exception during step execution
187
*/
188
void accept(T1 arg1, T2 arg2) throws Throwable;
189
}
190
191
@FunctionalInterface
192
interface A3<T1, T2, T3> {
193
void accept(T1 arg1, T2 arg2, T3 arg3) throws Throwable;
194
}
195
196
@FunctionalInterface
197
interface A4<T1, T2, T3, T4> {
198
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4) throws Throwable;
199
}
200
201
@FunctionalInterface
202
interface A5<T1, T2, T3, T4, T5> {
203
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) throws Throwable;
204
}
205
206
@FunctionalInterface
207
interface A6<T1, T2, T3, T4, T5, T6> {
208
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) throws Throwable;
209
}
210
211
@FunctionalInterface
212
interface A7<T1, T2, T3, T4, T5, T6, T7> {
213
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) throws Throwable;
214
}
215
216
@FunctionalInterface
217
interface A8<T1, T2, T3, T4, T5, T6, T7, T8> {
218
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) throws Throwable;
219
}
220
221
@FunctionalInterface
222
interface A9<T1, T2, T3, T4, T5, T6, T7, T8, T9> {
223
void accept(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) throws Throwable;
224
}
225
}
226
```
227
228
**Parameter Extraction:**
229
230
Parameters are automatically extracted from step expressions using Cucumber's parameter matching:
231
232
```java
233
// String parameter extraction
234
When("I enter {string}", (String text) -> { ... });
235
236
// Numeric parameter extraction
237
When("I wait {int} seconds", (Integer seconds) -> { ... });
238
When("I set the value to {double}", (Double value) -> { ... });
239
240
// Boolean parameter extraction
241
When("the flag is {boolean}", (Boolean flag) -> { ... });
242
243
// Custom parameter types (defined with ParameterType)
244
When("I transfer {amount} to account {int}", (Amount amount, Integer accountId) -> { ... });
245
246
// Mixed parameter types
247
When("user {string} has {int} items costing {double} each",
248
(String username, Integer count, Double unitPrice) -> { ... });
249
```
250
251
### Error Handling
252
253
All step definition bodies allow throwing any exception, which will be caught and handled by the Cucumber framework:
254
255
```java
256
Given("a step that might fail", () -> {
257
if (someCondition) {
258
throw new RuntimeException("Step failed due to condition");
259
}
260
// Normal execution continues
261
});
262
263
// Use PendingException for unimplemented steps
264
Given("a step not yet implemented", () -> {
265
throw new PendingException("TODO: implement this step");
266
});
267
```