@@ -102,6 +102,9 @@ Annotate a field in your step definition class with `@Autowired`.
102
102
``` java
103
103
package com.example.app ;
104
104
105
+ import org.springframework.beans.factory.annotation.Autowired ;
106
+ import io.cucumber.java.en.Given ;
107
+
105
108
public class MyStepDefinitions {
106
109
107
110
@Autowired
@@ -162,6 +165,9 @@ The glue scoped component can then be autowired into a step definition:
162
165
``` java
163
166
package com.example.app ;
164
167
168
+ import org.springframework.beans.factory.annotation.Autowired ;
169
+ import io.cucumber.java.en.Given ;
170
+
165
171
public class UserStepDefinitions {
166
172
167
173
@Autowired
@@ -194,6 +200,65 @@ public class PurchaseStepDefinitions {
194
200
}
195
201
```
196
202
203
+ #### Sharing state between threads
204
+
205
+ By default, when using ` @ScenarioScope ` these beans must also be accessed on
206
+ the same thread as the one that is executing the scenario. If you are certain
207
+ your scenario scoped beans can only be accessed through step definitions you
208
+ can use ` @ScenarioScope(proxyMode = ScopedProxyMode.NO) ` .
209
+
210
+
211
+ ``` java
212
+ package com.example.app ;
213
+
214
+ import org.springframework.stereotype.Component ;
215
+ import io.cucumber.spring.ScenarioScope ;
216
+ import org.springframework.context.annotation.ScopedProxyMode ;
217
+
218
+ @Component
219
+ @ScenarioScope (proxyMode = ScopedProxyMode . NO )
220
+ public class TestUserInformation {
221
+
222
+ private User testUser;
223
+
224
+ public void setTestUser (User testUser ) {
225
+ this . testUser = testUser;
226
+ }
227
+
228
+ public User getTestUser () {
229
+ return testUser;
230
+ }
231
+
232
+ }
233
+ ```
234
+
235
+ ``` java
236
+ package com.example.app ;
237
+
238
+ import org.springframework.beans.factory.annotation.Autowired ;
239
+ import io.cucumber.java.en.Given ;
240
+ import org.awaitility.Awaitility ;
241
+
242
+ public class UserStepDefinitions {
243
+
244
+ @Autowired
245
+ private TestUserInformation testUserInformation;
246
+
247
+ @Then (" the test user is eventually created" )
248
+ public void a_user_is_eventually_created () {
249
+ Awaitility . await()
250
+ .untilAsserted(() - > {
251
+ // This happens on a different thread
252
+ TestUser testUser = testUserInformation. getTestUser();
253
+ Optional<User > user = repository. findById(testUser. getId());
254
+ assertTrue(user. isPresent());
255
+ });
256
+ }
257
+ }
258
+ ```
259
+
260
+
261
+
197
262
### Dirtying the application context
198
263
199
264
If your tests do dirty the application context you can add ` @DirtiesContext ` to
0 commit comments