|
1 | 1 | package io.cucumber.jakarta.cdi;
|
2 | 2 |
|
3 | 3 | import io.cucumber.core.backend.ObjectFactory;
|
| 4 | +import jakarta.enterprise.context.ApplicationScoped; |
| 5 | +import jakarta.enterprise.inject.Vetoed; |
4 | 6 | import org.junit.jupiter.api.Test;
|
5 | 7 |
|
6 | 8 | import static org.hamcrest.MatcherAssert.assertThat;
|
|
9 | 11 | import static org.hamcrest.core.IsNot.not;
|
10 | 12 | import static org.hamcrest.core.IsNull.notNullValue;
|
11 | 13 | import static org.junit.jupiter.api.Assertions.assertAll;
|
| 14 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
12 | 15 |
|
13 | 16 | class CdiJakartaFactoryTest {
|
14 | 17 |
|
15 |
| - @Test |
16 |
| - void shouldGiveUsNewInstancesForEachScenario() { |
| 18 | + final ObjectFactory factory = new CdiJakartaFactory(); |
| 19 | + |
| 20 | + @Vetoed |
| 21 | + static class VetoedBean { |
| 22 | + |
| 23 | + } |
17 | 24 |
|
18 |
| - final ObjectFactory factory = new CdiJakartaFactory(); |
19 |
| - factory.addClass(BellyStepDefinitions.class); |
20 |
| - factory.addClass(CdiBellyStepDefinitions.class); |
| 25 | + @Test |
| 26 | + void shouldCreateNewInstancesForEachScenario() { |
| 27 | + factory.addClass(VetoedBean.class); |
21 | 28 |
|
22 | 29 | // Scenario 1
|
23 | 30 | factory.start();
|
24 |
| - final BellyStepDefinitions o1 = factory.getInstance(BellyStepDefinitions.class); |
25 |
| - final CdiBellyStepDefinitions cdiStep = factory.getInstance(CdiBellyStepDefinitions.class); |
26 |
| - assertAll( |
27 |
| - // assert that it is is a CDI proxy |
28 |
| - () -> assertThat(cdiStep.getClass(), not(is(CdiBellyStepDefinitions.class))), |
29 |
| - () -> assertThat(cdiStep.getClass().getSuperclass(), is(CdiBellyStepDefinitions.class))); |
| 31 | + VetoedBean a1 = factory.getInstance(VetoedBean.class); |
| 32 | + VetoedBean a2 = factory.getInstance(VetoedBean.class); |
| 33 | + assertThat(a1, is(equalTo(a2))); |
30 | 34 | factory.stop();
|
31 | 35 |
|
32 | 36 | // Scenario 2
|
33 | 37 | factory.start();
|
34 |
| - final BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class); |
| 38 | + VetoedBean b1 = factory.getInstance(VetoedBean.class); |
35 | 39 | factory.stop();
|
36 | 40 |
|
| 41 | + // VetoedBean makes it possible to compare the object outside the |
| 42 | + // scenario/application scope |
| 43 | + assertAll( |
| 44 | + () -> assertThat(a1, is(notNullValue())), |
| 45 | + () -> assertThat(a1, is(not(equalTo(b1)))), |
| 46 | + () -> assertThat(b1, is(not(equalTo(a1))))); |
| 47 | + } |
| 48 | + |
| 49 | + @ApplicationScoped |
| 50 | + static class ApplicationScopedBean { |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldCreateApplicationScopedInstance() { |
| 56 | + factory.addClass(ApplicationScopedBean.class); |
| 57 | + factory.start(); |
| 58 | + ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class); |
37 | 59 | assertAll(
|
38 |
| - () -> assertThat(o1, is(notNullValue())), |
39 |
| - () -> assertThat(o1, is(not(equalTo(o2)))), |
40 |
| - () -> assertThat(o2, is(not(equalTo(o1))))); |
| 60 | + // assert that it is is a CDI proxy |
| 61 | + () -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))), |
| 62 | + () -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class))); |
| 63 | + factory.stop(); |
41 | 64 | }
|
42 | 65 |
|
| 66 | + @Test |
| 67 | + void shouldCreateUnmanagedInstance() { |
| 68 | + factory.addClass(UnmanagedBean.class); |
| 69 | + factory.start(); |
| 70 | + assertNotNull(factory.getInstance(UnmanagedBean.class)); |
| 71 | + UnmanagedBean cdiStep = factory.getInstance(UnmanagedBean.class); |
| 72 | + assertThat(cdiStep.getClass(), is(UnmanagedBean.class)); |
| 73 | + factory.stop(); |
| 74 | + } |
| 75 | + |
| 76 | + static class UnmanagedBean { |
| 77 | + |
| 78 | + } |
43 | 79 | }
|
0 commit comments