18
18
import io .cucumber .spring .metaconfig .general .BellyMetaStepDefinitions ;
19
19
import org .junit .jupiter .api .Test ;
20
20
import org .junit .jupiter .api .function .Executable ;
21
+ import org .junit .jupiter .params .ParameterizedTest ;
22
+ import org .junit .jupiter .params .provider .ValueSource ;
21
23
import org .springframework .beans .factory .BeanCreationException ;
22
24
import org .springframework .beans .factory .annotation .Autowired ;
23
25
import org .springframework .beans .factory .annotation .Value ;
24
26
import org .springframework .test .context .ContextConfiguration ;
27
+ import org .springframework .test .context .TestContext ;
28
+ import org .springframework .test .context .TestExecutionListener ;
29
+ import org .springframework .test .context .TestExecutionListeners ;
25
30
26
31
import java .util .Optional ;
27
32
35
40
import static org .junit .jupiter .api .Assertions .assertAll ;
36
41
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
37
42
import static org .junit .jupiter .api .Assertions .assertEquals ;
38
- import static org .junit .jupiter .api .Assertions .assertNull ;
39
43
import static org .junit .jupiter .api .Assertions .assertThrows ;
40
44
import static org .junit .jupiter .api .Assertions .assertTrue ;
41
45
@@ -57,10 +61,10 @@ void shouldGiveUsNewStepInstancesForEachScenario() {
57
61
factory .stop ();
58
62
59
63
assertAll (
60
- () -> assertThat (o1 , is (notNullValue ())),
61
- () -> assertThat (o2 , is (notNullValue ())),
62
- () -> assertThat (o1 , is (not (equalTo (o2 )))),
63
- () -> assertThat (o2 , is (not (equalTo (o1 )))));
64
+ () -> assertThat (o1 , is (notNullValue ())),
65
+ () -> assertThat (o2 , is (notNullValue ())),
66
+ () -> assertThat (o1 , is (not (equalTo (o2 )))),
67
+ () -> assertThat (o2 , is (not (equalTo (o1 )))));
64
68
}
65
69
66
70
@ Test
@@ -101,10 +105,10 @@ void shouldNeverCreateNewApplicationBeanInstances() {
101
105
factory2 .stop ();
102
106
103
107
assertAll (
104
- () -> assertThat (o1 , is (notNullValue ())),
105
- () -> assertThat (o2 , is (notNullValue ())),
106
- () -> assertThat (o1 , is (equalTo (o1 ))),
107
- () -> assertThat (o2 , is (equalTo (o2 ))));
108
+ () -> assertThat (o1 , is (notNullValue ())),
109
+ () -> assertThat (o2 , is (notNullValue ())),
110
+ () -> assertThat (o1 , is (equalTo (o1 ))),
111
+ () -> assertThat (o2 , is (equalTo (o2 ))));
108
112
}
109
113
110
114
@ Test
@@ -124,10 +128,10 @@ void shouldNeverCreateNewApplicationBeanInstancesUsingMetaConfiguration() {
124
128
factory2 .stop ();
125
129
126
130
assertAll (
127
- () -> assertThat (o1 , is (notNullValue ())),
128
- () -> assertThat (o2 , is (notNullValue ())),
129
- () -> assertThat (o1 , is (equalTo (o1 ))),
130
- () -> assertThat (o2 , is (equalTo (o2 ))));
131
+ () -> assertThat (o1 , is (notNullValue ())),
132
+ () -> assertThat (o2 , is (notNullValue ())),
133
+ () -> assertThat (o1 , is (equalTo (o1 ))),
134
+ () -> assertThat (o2 , is (equalTo (o2 ))));
131
135
}
132
136
133
137
@ Test
@@ -143,10 +147,10 @@ void shouldFindStepDefsCreatedImplicitlyForAutowiring() {
143
147
factory1 .stop ();
144
148
145
149
assertAll (
146
- () -> assertThat (o1 .getThirdStepDef (), is (notNullValue ())),
147
- () -> assertThat (o2 , is (notNullValue ())),
148
- () -> assertThat (o1 .getThirdStepDef (), is (equalTo (o2 ))),
149
- () -> assertThat (o2 , is (equalTo (o1 .getThirdStepDef ()))));
150
+ () -> assertThat (o1 .getThirdStepDef (), is (notNullValue ())),
151
+ () -> assertThat (o2 , is (notNullValue ())),
152
+ () -> assertThat (o1 .getThirdStepDef (), is (equalTo (o2 ))),
153
+ () -> assertThat (o2 , is (equalTo (o1 .getThirdStepDef ()))));
150
154
}
151
155
152
156
@ Test
@@ -162,10 +166,10 @@ void shouldReuseStepDefsCreatedImplicitlyForAutowiring() {
162
166
factory1 .stop ();
163
167
164
168
assertAll (
165
- () -> assertThat (o1 .getThirdStepDef (), is (notNullValue ())),
166
- () -> assertThat (o3 .getThirdStepDef (), is (notNullValue ())),
167
- () -> assertThat (o1 .getThirdStepDef (), is (equalTo (o3 .getThirdStepDef ()))),
168
- () -> assertThat (o3 .getThirdStepDef (), is (equalTo (o1 .getThirdStepDef ()))));
169
+ () -> assertThat (o1 .getThirdStepDef (), is (notNullValue ())),
170
+ () -> assertThat (o3 .getThirdStepDef (), is (notNullValue ())),
171
+ () -> assertThat (o1 .getThirdStepDef (), is (equalTo (o3 .getThirdStepDef ()))),
172
+ () -> assertThat (o3 .getThirdStepDef (), is (equalTo (o1 .getThirdStepDef ()))));
169
173
}
170
174
171
175
@ Test
@@ -209,10 +213,10 @@ void shouldRespectDirtiesContextAnnotationsInStepDefs() {
209
213
factory .stop ();
210
214
211
215
assertAll (
212
- () -> assertThat (o1 , is (notNullValue ())),
213
- () -> assertThat (o2 , is (notNullValue ())),
214
- () -> assertThat (o1 , is (not (equalTo (o2 )))),
215
- () -> assertThat (o2 , is (not (equalTo (o1 )))));
216
+ () -> assertThat (o1 , is (notNullValue ())),
217
+ () -> assertThat (o2 , is (notNullValue ())),
218
+ () -> assertThat (o1 , is (not (equalTo (o2 )))),
219
+ () -> assertThat (o2 , is (not (equalTo (o1 )))));
216
220
}
217
221
218
222
@ Test
@@ -232,10 +236,10 @@ void shouldRespectDirtiesContextAnnotationsInStepDefsUsingMetaConfiguration() {
232
236
factory .stop ();
233
237
234
238
assertAll (
235
- () -> assertThat (o1 , is (notNullValue ())),
236
- () -> assertThat (o2 , is (notNullValue ())),
237
- () -> assertThat (o1 , is (not (equalTo (o2 )))),
238
- () -> assertThat (o2 , is (not (equalTo (o1 )))));
239
+ () -> assertThat (o1 , is (notNullValue ())),
240
+ () -> assertThat (o2 , is (notNullValue ())),
241
+ () -> assertThat (o1 , is (not (equalTo (o2 )))),
242
+ () -> assertThat (o2 , is (not (equalTo (o1 )))));
239
243
}
240
244
241
245
@ Test
@@ -257,9 +261,9 @@ void shouldFailIfMultipleClassesWithSpringAnnotationsAreFound() {
257
261
Executable testMethod = () -> factory .addClass (BellyStepDefinitions .class );
258
262
CucumberBackendException actualThrown = assertThrows (CucumberBackendException .class , testMethod );
259
263
assertThat (actualThrown .getMessage (), startsWith (
260
- "Glue class class io.cucumber.spring.contextconfig.BellyStepDefinitions and class io.cucumber.spring.SpringFactoryTest$WithSpringAnnotations are both annotated with @CucumberContextConfiguration.\n "
261
- +
262
- "Please ensure only one class configures the spring context" ));
264
+ "Glue class class io.cucumber.spring.contextconfig.BellyStepDefinitions and class io.cucumber.spring.SpringFactoryTest$WithSpringAnnotations are both annotated with @CucumberContextConfiguration.\n "
265
+ +
266
+ "Please ensure only one class configures the spring context" ));
263
267
}
264
268
265
269
@ Test
@@ -269,7 +273,7 @@ void shouldFailIfClassWithSpringComponentAnnotationsIsFound() {
269
273
Executable testMethod = () -> factory .addClass (WithComponentAnnotation .class );
270
274
CucumberBackendException actualThrown = assertThrows (CucumberBackendException .class , testMethod );
271
275
assertThat (actualThrown .getMessage (), is (equalTo (
272
- "Glue class io.cucumber.spring.componentannotation.WithComponentAnnotation was annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component annotation" )));
276
+ "Glue class io.cucumber.spring.componentannotation.WithComponentAnnotation was annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component annotation" )));
273
277
}
274
278
275
279
@ Test
@@ -279,7 +283,7 @@ void shouldFailIfClassWithAnnotationAnnotatedWithSpringComponentAnnotationsIsFou
279
283
Executable testMethod = () -> factory .addClass (WithControllerAnnotation .class );
280
284
CucumberBackendException actualThrown = assertThrows (CucumberBackendException .class , testMethod );
281
285
assertThat (actualThrown .getMessage (), is (equalTo (
282
- "Glue class io.cucumber.spring.componentannotation.WithControllerAnnotation was annotated with @Controller; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Controller annotation" )));
286
+ "Glue class io.cucumber.spring.componentannotation.WithControllerAnnotation was annotated with @Controller; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Controller annotation" )));
283
287
}
284
288
285
289
@ Test
@@ -300,10 +304,10 @@ void shouldGlueScopedSpringBeanBehaveLikeGlueLifecycle() {
300
304
factory .stop ();
301
305
302
306
assertAll (
303
- () -> assertThat (glueInstance1 , is (not (glueInstance2 ))),
304
- () -> assertThat (glueInstance2 , is (not (glueInstance1 ))),
305
- () -> assertThat (bellyInstance1 , is (bellyInstance2 )),
306
- () -> assertThat (bellyInstance2 , is (bellyInstance1 )));
307
+ () -> assertThat (glueInstance1 , is (not (glueInstance2 ))),
308
+ () -> assertThat (glueInstance2 , is (not (glueInstance1 ))),
309
+ () -> assertThat (bellyInstance1 , is (bellyInstance2 )),
310
+ () -> assertThat (bellyInstance2 , is (bellyInstance1 )));
307
311
}
308
312
309
313
@ Test
@@ -327,7 +331,7 @@ void shouldBeStoppableWhenFacedWithInvalidConfiguration() {
327
331
328
332
IllegalStateException exception = assertThrows (IllegalStateException .class , factory ::start );
329
333
assertThat (exception .getMessage (),
330
- containsString ("DelegatingSmartContextLoader was unable to detect defaults" ));
334
+ containsString ("DelegatingSmartContextLoader was unable to detect defaults" ));
331
335
assertDoesNotThrow (factory ::stop );
332
336
}
333
337
@@ -341,6 +345,19 @@ void shouldBeStoppableWhenFacedWithMissingContextConfiguration() {
341
345
assertDoesNotThrow (factory ::stop );
342
346
}
343
347
348
+ @ ParameterizedTest
349
+ @ ValueSource (classes = {
350
+ FailedBeforeTestClassContextConfiguration .class ,
351
+ FailedBeforeTestMethodContextConfiguration .class ,
352
+ })
353
+ void shouldBeStoppableWhenFacedWithFailedApplicationContext (Class <?> contextConfiguration ) {
354
+ final ObjectFactory factory = new SpringFactory ();
355
+ factory .addClass (contextConfiguration );
356
+
357
+ assertThrows (CucumberBackendException .class , factory ::start );
358
+ assertDoesNotThrow (factory ::stop );
359
+ }
360
+
344
361
@ CucumberContextConfiguration
345
362
@ ContextConfiguration ("classpath:cucumber.xml" )
346
363
public static class WithSpringAnnotations {
@@ -366,7 +383,7 @@ public String getProperty() {
366
383
}
367
384
368
385
@ CucumberContextConfiguration
369
- @ ContextConfiguration ()
386
+ @ ContextConfiguration
370
387
public static class WithEmptySpringAnnotations {
371
388
372
389
}
@@ -376,4 +393,39 @@ public static class WithoutContextConfiguration {
376
393
377
394
}
378
395
396
+ @ CucumberContextConfiguration
397
+ @ ContextConfiguration ("classpath:cucumber.xml" )
398
+ @ TestExecutionListeners (FailedBeforeTestClassContextConfiguration .FailingListener .class )
399
+ public static class FailedBeforeTestClassContextConfiguration {
400
+
401
+ public static class FailingListener implements TestExecutionListener {
402
+
403
+ @ Override
404
+ public void beforeTestClass (TestContext testContext ) throws Exception {
405
+ throw new StubException ();
406
+ }
407
+
408
+ }
409
+
410
+ }
411
+
412
+ @ CucumberContextConfiguration
413
+ @ ContextConfiguration ("classpath:cucumber.xml" )
414
+ @ TestExecutionListeners (FailedBeforeTestMethodContextConfiguration .FailingListener .class )
415
+ public static class FailedBeforeTestMethodContextConfiguration {
416
+
417
+ public static class FailingListener implements TestExecutionListener {
418
+
419
+ @ Override
420
+ public void beforeTestMethod (TestContext testContext ) throws Exception {
421
+ throw new StubException ();
422
+ }
423
+
424
+ }
425
+
426
+ }
427
+
428
+ public static class StubException extends Exception {
429
+
430
+ }
379
431
}
0 commit comments