|
1 | 1 | package io.cucumber.cdi2;
|
2 | 2 |
|
3 | 3 | import io.cucumber.core.backend.ObjectFactory;
|
| 4 | +import org.junit.jupiter.api.AfterEach; |
4 | 5 | import org.junit.jupiter.api.Test;
|
5 | 6 |
|
6 | 7 | import javax.enterprise.context.ApplicationScoped;
|
7 | 8 | import javax.enterprise.inject.Vetoed;
|
| 9 | +import javax.inject.Inject; |
8 | 10 |
|
9 | 11 | import static org.hamcrest.MatcherAssert.assertThat;
|
10 | 12 | import static org.hamcrest.core.Is.is;
|
11 | 13 | import static org.hamcrest.core.IsEqual.equalTo;
|
12 | 14 | import static org.hamcrest.core.IsNot.not;
|
13 | 15 | import static org.hamcrest.core.IsNull.notNullValue;
|
14 | 16 | import static org.junit.jupiter.api.Assertions.assertAll;
|
15 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
16 | 18 |
|
17 | 19 | class Cdi2FactoryTest {
|
18 | 20 |
|
19 | 21 | final ObjectFactory factory = new Cdi2Factory();
|
20 | 22 |
|
| 23 | + @AfterEach |
| 24 | + void stop(){ |
| 25 | + factory.stop(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void lifecycleIsIdempotent(){ |
| 30 | + assertDoesNotThrow(factory::stop); |
| 31 | + factory.start(); |
| 32 | + assertDoesNotThrow(factory::start); |
| 33 | + factory.stop(); |
| 34 | + assertDoesNotThrow(factory::stop); |
| 35 | + } |
| 36 | + |
21 | 37 | @Vetoed
|
22 | 38 | static class VetoedBean {
|
23 | 39 |
|
24 | 40 | }
|
25 | 41 |
|
26 | 42 | @Test
|
27 | 43 | void shouldCreateNewInstancesForEachScenario() {
|
28 |
| - factory.addClass(VetoedBean.class); |
29 |
| - |
30 | 44 | // Scenario 1
|
31 | 45 | factory.start();
|
32 | 46 | VetoedBean a1 = factory.getInstance(VetoedBean.class);
|
@@ -54,28 +68,46 @@ static class ApplicationScopedBean {
|
54 | 68 |
|
55 | 69 | @Test
|
56 | 70 | void shouldCreateApplicationScopedInstance() {
|
57 |
| - factory.addClass(ApplicationScopedBean.class); |
58 | 71 | factory.start();
|
59 |
| - ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class); |
| 72 | + ApplicationScopedBean bean = factory.getInstance(ApplicationScopedBean.class); |
60 | 73 | assertAll(
|
61 | 74 | // assert that it is is a CDI proxy
|
62 |
| - () -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))), |
63 |
| - () -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class))); |
| 75 | + () -> assertThat(bean.getClass(), not(is(ApplicationScopedBean.class))), |
| 76 | + () -> assertThat(bean.getClass().getSuperclass(), is(ApplicationScopedBean.class))); |
64 | 77 | factory.stop();
|
65 | 78 | }
|
66 | 79 |
|
| 80 | + static class UnmanagedBean { |
| 81 | + |
| 82 | + } |
| 83 | + |
67 | 84 | @Test
|
68 | 85 | void shouldCreateUnmanagedInstance() {
|
69 |
| - factory.addClass(UnmanagedBean.class); |
70 | 86 | factory.start();
|
71 |
| - assertNotNull(factory.getInstance(UnmanagedBean.class)); |
72 |
| - UnmanagedBean cdiStep = factory.getInstance(UnmanagedBean.class); |
73 |
| - assertThat(cdiStep.getClass(), is(UnmanagedBean.class)); |
| 87 | + UnmanagedBean bean = factory.getInstance(UnmanagedBean.class); |
| 88 | + assertThat(bean.getClass(), is(UnmanagedBean.class)); |
74 | 89 | factory.stop();
|
75 | 90 | }
|
76 | 91 |
|
77 |
| - static class UnmanagedBean { |
| 92 | + static class OtherStepDefinitions { |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + static class StepDefinitions { |
| 97 | + |
| 98 | + @Inject |
| 99 | + OtherStepDefinitions injected; |
78 | 100 |
|
79 | 101 | }
|
80 | 102 |
|
| 103 | + @Test |
| 104 | + void shouldInjectStepDefinitions() { |
| 105 | + factory.addClass(OtherStepDefinitions.class); |
| 106 | + factory.addClass(StepDefinitions.class); |
| 107 | + factory.start(); |
| 108 | + StepDefinitions stepDefinitions = factory.getInstance(StepDefinitions.class); |
| 109 | + assertThat(stepDefinitions.injected, is(notNullValue())); |
| 110 | + factory.stop(); |
| 111 | + } |
| 112 | + |
81 | 113 | }
|
0 commit comments