|
18 | 18 |
|
19 | 19 | import java.util.concurrent.atomic.AtomicInteger;
|
20 | 20 |
|
21 |
| -import org.junit.jupiter.api.AfterAll; |
22 | 21 | import org.junit.jupiter.api.AfterEach;
|
23 |
| -import org.junit.jupiter.api.BeforeAll; |
24 | 22 | import org.junit.jupiter.api.BeforeEach;
|
25 |
| -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
26 | 23 | import org.junit.jupiter.api.Test;
|
27 |
| -import org.junit.jupiter.api.TestMethodOrder; |
28 | 24 | import org.junit.platform.testkit.engine.EngineTestKit;
|
29 | 25 |
|
30 | 26 | import org.springframework.context.annotation.Bean;
|
|
49 | 45 | */
|
50 | 46 | class ContextFailureThresholdTests {
|
51 | 47 |
|
52 |
| - private static final AtomicInteger loadCount = new AtomicInteger(0); |
| 48 | + private static final AtomicInteger passingLoadCount = new AtomicInteger(0); |
| 49 | + private static final AtomicInteger failingLoadCount = new AtomicInteger(0); |
53 | 50 |
|
54 | 51 |
|
55 | 52 | @BeforeEach
|
56 | 53 | @AfterEach
|
57 | 54 | void resetFlag() {
|
58 |
| - loadCount.set(0); |
| 55 | + resetContextCache(); |
| 56 | + passingLoadCount.set(0); |
| 57 | + failingLoadCount.set(0); |
59 | 58 | SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, null);
|
60 | 59 | }
|
61 | 60 |
|
62 | 61 | @Test
|
63 | 62 | void defaultThreshold() {
|
64 |
| - assertThat(loadCount.get()).isZero(); |
65 |
| - |
66 |
| - EngineTestKit.engine("junit-jupiter")// |
67 |
| - .selectors(selectClass(PassingTestCase.class))// 2 passing |
68 |
| - .selectors(selectClass(FailingTestCase.class))// 3 failing |
69 |
| - .execute()// |
70 |
| - .testEvents()// |
71 |
| - .assertStatistics(stats -> stats.started(5).succeeded(2).failed(3)); |
72 |
| - assertThat(loadCount.get()).isEqualTo(DEFAULT_CONTEXT_FAILURE_THRESHOLD); |
| 63 | + runTests(); |
| 64 | + assertThat(passingLoadCount.get()).isEqualTo(1); |
| 65 | + assertThat(failingLoadCount.get()).isEqualTo(DEFAULT_CONTEXT_FAILURE_THRESHOLD); |
73 | 66 | }
|
74 | 67 |
|
75 | 68 | @Test
|
76 | 69 | void customThreshold() {
|
77 |
| - assertThat(loadCount.get()).isZero(); |
| 70 | + int customThreshold = 2; |
| 71 | + SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, Integer.toString(customThreshold)); |
78 | 72 |
|
79 |
| - int threshold = 2; |
80 |
| - SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, Integer.toString(threshold)); |
81 |
| - |
82 |
| - EngineTestKit.engine("junit-jupiter")// |
83 |
| - .selectors(selectClass(PassingTestCase.class))// 2 passing |
84 |
| - .selectors(selectClass(FailingTestCase.class))// 3 failing |
85 |
| - .execute()// |
86 |
| - .testEvents()// |
87 |
| - .assertStatistics(stats -> stats.started(5).succeeded(2).failed(3)); |
88 |
| - assertThat(loadCount.get()).isEqualTo(threshold); |
| 73 | + runTests(); |
| 74 | + assertThat(passingLoadCount.get()).isEqualTo(1); |
| 75 | + assertThat(failingLoadCount.get()).isEqualTo(customThreshold); |
89 | 76 | }
|
90 | 77 |
|
91 | 78 | @Test
|
92 | 79 | void thresholdEffectivelyDisabled() {
|
93 |
| - assertThat(loadCount.get()).isZero(); |
94 |
| - |
95 | 80 | SpringProperties.setProperty(CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, "999999");
|
96 | 81 |
|
| 82 | + runTests(); |
| 83 | + assertThat(passingLoadCount.get()).isEqualTo(1); |
| 84 | + assertThat(failingLoadCount.get()).isEqualTo(6); |
| 85 | + } |
| 86 | + |
| 87 | + private static void runTests() { |
97 | 88 | EngineTestKit.engine("junit-jupiter")//
|
98 |
| - .selectors(selectClass(PassingTestCase.class))// 2 passing |
99 |
| - .selectors(selectClass(FailingTestCase.class))// 3 failing |
100 |
| - .execute()// |
101 |
| - .testEvents()// |
102 |
| - .assertStatistics(stats -> stats.started(5).succeeded(2).failed(3)); |
103 |
| - assertThat(loadCount.get()).isEqualTo(3); |
| 89 | + .selectors(// |
| 90 | + selectClass(PassingTestCase.class), // 3 passing |
| 91 | + selectClass(FailingConfigTestCase.class), // 3 failing |
| 92 | + selectClass(SharedFailingConfigTestCase.class) // 3 failing |
| 93 | + )// |
| 94 | + .execute()// |
| 95 | + .testEvents()// |
| 96 | + .assertStatistics(stats -> stats.started(9).succeeded(3).failed(6)); |
| 97 | + assertContextCacheStatistics(1, 2, (1 + 3 + 3)); |
104 | 98 | }
|
105 | 99 |
|
106 | 100 |
|
107 |
| - @SpringJUnitConfig |
108 | 101 | @TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
|
109 |
| - static class PassingTestCase { |
110 |
| - |
111 |
| - @BeforeAll |
112 |
| - static void verifyInitialCacheState() { |
113 |
| - resetContextCache(); |
114 |
| - assertContextCacheStatistics("BeforeAll", 0, 0, 0); |
115 |
| - } |
116 |
| - |
117 |
| - @AfterAll |
118 |
| - static void verifyFinalCacheState() { |
119 |
| - assertContextCacheStatistics("AfterAll", 1, 1, 1); |
120 |
| - resetContextCache(); |
121 |
| - } |
| 102 | + static abstract class BaseTestCase { |
122 | 103 |
|
123 | 104 | @Test
|
124 | 105 | void test1() {}
|
125 | 106 |
|
126 | 107 | @Test
|
127 | 108 | void test2() {}
|
128 | 109 |
|
129 |
| - @Configuration |
130 |
| - static class PassingConfig { |
131 |
| - } |
| 110 | + @Test |
| 111 | + void test3() {} |
132 | 112 | }
|
133 | 113 |
|
134 |
| - @SpringJUnitConfig |
135 |
| - @TestExecutionListeners(DependencyInjectionTestExecutionListener.class) |
136 |
| - @TestMethodOrder(OrderAnnotation.class) |
137 |
| - static class FailingTestCase { |
138 |
| - |
139 |
| - @BeforeAll |
140 |
| - static void verifyInitialCacheState() { |
141 |
| - resetContextCache(); |
142 |
| - assertContextCacheStatistics("BeforeAll", 0, 0, 0); |
143 |
| - } |
| 114 | + @SpringJUnitConfig(PassingConfig.class) |
| 115 | + static class PassingTestCase extends BaseTestCase { |
| 116 | + } |
144 | 117 |
|
145 |
| - @AfterAll |
146 |
| - static void verifyFinalCacheState() { |
147 |
| - assertContextCacheStatistics("AfterAll", 0, 0, 3); |
148 |
| - resetContextCache(); |
149 |
| - } |
| 118 | + @SpringJUnitConfig(FailingConfig.class) |
| 119 | + static class FailingConfigTestCase extends BaseTestCase { |
| 120 | + } |
150 | 121 |
|
151 |
| - @Test |
152 |
| - void test1() {} |
| 122 | + @SpringJUnitConfig(FailingConfig.class) |
| 123 | + static class SharedFailingConfigTestCase extends BaseTestCase { |
| 124 | + } |
153 | 125 |
|
154 |
| - @Test |
155 |
| - void test2() {} |
| 126 | + @Configuration |
| 127 | + static class PassingConfig { |
156 | 128 |
|
157 |
| - @Test |
158 |
| - void test3() {} |
| 129 | + PassingConfig() { |
| 130 | + passingLoadCount.incrementAndGet(); |
| 131 | + } |
| 132 | + } |
159 | 133 |
|
160 |
| - @Configuration |
161 |
| - static class FailingConfig { |
| 134 | + @Configuration |
| 135 | + static class FailingConfig { |
162 | 136 |
|
163 |
| - FailingConfig() { |
164 |
| - loadCount.incrementAndGet(); |
165 |
| - } |
| 137 | + FailingConfig() { |
| 138 | + failingLoadCount.incrementAndGet(); |
| 139 | + } |
166 | 140 |
|
167 |
| - @Bean |
168 |
| - String explosiveString() { |
169 |
| - throw new RuntimeException("Boom!"); |
170 |
| - } |
| 141 | + @Bean |
| 142 | + String explosiveString() { |
| 143 | + throw new RuntimeException("Boom!"); |
171 | 144 | }
|
172 | 145 | }
|
173 | 146 |
|
|
0 commit comments