|
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.springframework.beans.factory.BeanCreationException; |
21 | 22 | import org.springframework.beans.factory.annotation.Autowired;
|
22 | 23 | import org.springframework.beans.factory.annotation.Value;
|
23 | 24 | import org.springframework.test.context.ContextConfiguration;
|
@@ -262,26 +263,35 @@ void shouldGlueScopedSpringBeanBehaveLikeGlueLifecycle() {
|
262 | 263 |
|
263 | 264 | // Scenario 1
|
264 | 265 | factory.start();
|
265 |
| - final Belly belly1 = factory.getInstance(Belly.class); |
266 |
| - final GlueScopedComponent glue1 = factory.getInstance(GlueScopedComponent.class); |
267 |
| - |
268 |
| - assertAll( |
269 |
| - () -> assertThat(belly1, is(notNullValue())), |
270 |
| - () -> assertThat(glue1, is(notNullValue()))); |
271 |
| - |
| 266 | + long bellyInstance1 = factory.getInstance(Belly.class).getInstanceId(); |
| 267 | + long glueInstance1 = factory.getInstance(GlueScopedComponent.class).getInstanceId(); |
272 | 268 | factory.stop();
|
273 | 269 |
|
274 | 270 | // Scenario 2
|
275 |
| - final Belly belly2 = factory.getInstance(Belly.class); |
276 |
| - final GlueScopedComponent glue2 = factory.getInstance(GlueScopedComponent.class); |
| 271 | + factory.start(); |
| 272 | + long bellyInstance2 = factory.getInstance(Belly.class).getInstanceId(); |
| 273 | + long glueInstance2 = factory.getInstance(GlueScopedComponent.class).getInstanceId(); |
| 274 | + factory.stop(); |
277 | 275 |
|
278 | 276 | assertAll(
|
279 |
| - () -> assertThat(belly2, is(notNullValue())), |
280 |
| - () -> assertThat(glue2, is(notNullValue())), |
281 |
| - () -> assertThat(glue1, is(not(equalTo(glue2)))), |
282 |
| - () -> assertThat(glue2, is(not(equalTo(glue1)))), |
283 |
| - () -> assertThat(belly1, is(equalTo(belly2))), |
284 |
| - () -> assertThat(belly2, is(equalTo(belly1)))); |
| 277 | + () -> assertThat(glueInstance1, is(not(glueInstance2))), |
| 278 | + () -> assertThat(glueInstance2, is(not(glueInstance1))), |
| 279 | + () -> assertThat(bellyInstance1, is(bellyInstance2)), |
| 280 | + () -> assertThat(bellyInstance2, is(bellyInstance1))); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + void shouldThrowWhenGlueScopedSpringBeanAreUsedOutsideLifecycle() { |
| 285 | + final ObjectFactory factory = new SpringFactory(); |
| 286 | + factory.addClass(WithSpringAnnotations.class); |
| 287 | + |
| 288 | + factory.start(); |
| 289 | + final Belly belly = factory.getInstance(Belly.class); |
| 290 | + final GlueScopedComponent glue = factory.getInstance(GlueScopedComponent.class); |
| 291 | + factory.stop(); |
| 292 | + |
| 293 | + assertDoesNotThrow(belly::getInstanceId); |
| 294 | + assertThrows(BeanCreationException.class, glue::getInstanceId); |
285 | 295 | }
|
286 | 296 |
|
287 | 297 | @Test
|
|
0 commit comments