|
1 | 1 | package io.cucumber.spring; |
2 | 2 |
|
3 | 3 | import io.cucumber.core.backend.CucumberBackendException; |
| 4 | +import io.cucumber.spring.beans.BellyBean; |
| 5 | +import io.cucumber.spring.beans.DummyComponent; |
4 | 6 | import org.junit.jupiter.api.AfterEach; |
5 | 7 | import org.junit.jupiter.api.Test; |
6 | 8 | import org.junit.jupiter.api.extension.ExtendWith; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.ValueSource; |
7 | 11 | import org.mockito.InOrder; |
8 | 12 | import org.mockito.Mock; |
9 | 13 | import org.mockito.Mockito; |
10 | 14 | import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | +import org.springframework.beans.factory.BeanNameAware; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.lang.NonNull; |
11 | 18 | import org.springframework.test.context.ContextConfiguration; |
12 | 19 | import org.springframework.test.context.TestContextManager; |
13 | 20 | import org.springframework.test.context.TestExecutionListener; |
14 | 21 |
|
15 | 22 | import static java.util.Collections.singletonList; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertSame; |
16 | 28 | import static org.junit.jupiter.api.Assertions.assertThrows; |
17 | 29 | import static org.mockito.ArgumentMatchers.any; |
18 | 30 | import static org.mockito.Mockito.doThrow; |
@@ -182,10 +194,125 @@ void invokesAllMethodsPriorIfAfterTestClassThrows() throws Exception { |
182 | 194 | inOrder.verify(listener).afterTestClass(any()); |
183 | 195 | } |
184 | 196 |
|
| 197 | + @ParameterizedTest |
| 198 | + @ValueSource(classes = { WithAutowiredDependency.class, WithConstructorDependency.class }) |
| 199 | + void autowireAndPostProcessesOnlyOnce(Class<? extends Spy> testClass) { |
| 200 | + TestContextManager manager = new TestContextManager(testClass); |
| 201 | + TestContextAdaptor adaptor = new TestContextAdaptor(manager, singletonList(testClass)); |
| 202 | + |
| 203 | + assertAll( |
| 204 | + () -> assertDoesNotThrow(adaptor::start), |
| 205 | + () -> assertNotNull(manager.getTestContext().getTestInstance()), |
| 206 | + () -> assertSame(manager.getTestContext().getTestInstance(), adaptor.getInstance(testClass)), |
| 207 | + () -> assertEquals(1, adaptor.getInstance(testClass).autowiredCount()), |
| 208 | + () -> assertEquals(1, adaptor.getInstance(testClass).postProcessedCount()), |
| 209 | + () -> assertNotNull(adaptor.getInstance(testClass).getBelly()), |
| 210 | + () -> assertNotNull(adaptor.getInstance(testClass).getDummyComponent()), |
| 211 | + () -> assertDoesNotThrow(adaptor::stop)); |
| 212 | + } |
| 213 | + |
185 | 214 | @CucumberContextConfiguration |
186 | 215 | @ContextConfiguration("classpath:cucumber.xml") |
187 | 216 | public static class SomeContextConfiguration { |
188 | 217 |
|
189 | 218 | } |
190 | 219 |
|
| 220 | + private interface Spy { |
| 221 | + |
| 222 | + int postProcessedCount(); |
| 223 | + |
| 224 | + int autowiredCount(); |
| 225 | + |
| 226 | + BellyBean getBelly(); |
| 227 | + |
| 228 | + DummyComponent getDummyComponent(); |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + @CucumberContextConfiguration |
| 233 | + @ContextConfiguration("classpath:cucumber.xml") |
| 234 | + public static class WithAutowiredDependency implements BeanNameAware, Spy { |
| 235 | + |
| 236 | + @Autowired |
| 237 | + BellyBean belly; |
| 238 | + |
| 239 | + int postProcessedCount = 0; |
| 240 | + int autowiredCount = 0; |
| 241 | + |
| 242 | + private DummyComponent dummyComponent; |
| 243 | + |
| 244 | + @Autowired |
| 245 | + public void setDummyComponent(DummyComponent dummyComponent) { |
| 246 | + this.dummyComponent = dummyComponent; |
| 247 | + this.autowiredCount++; |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public void setBeanName(@NonNull String ignored) { |
| 252 | + postProcessedCount++; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public int postProcessedCount() { |
| 257 | + return postProcessedCount; |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public int autowiredCount() { |
| 262 | + return autowiredCount; |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public BellyBean getBelly() { |
| 267 | + return belly; |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public DummyComponent getDummyComponent() { |
| 272 | + return dummyComponent; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + @CucumberContextConfiguration |
| 277 | + @ContextConfiguration("classpath:cucumber.xml") |
| 278 | + public static class WithConstructorDependency implements BeanNameAware, Spy { |
| 279 | + |
| 280 | + final BellyBean belly; |
| 281 | + final DummyComponent dummyComponent; |
| 282 | + |
| 283 | + int postProcessedCount = 0; |
| 284 | + int autowiredCount = 0; |
| 285 | + |
| 286 | + public WithConstructorDependency(BellyBean belly, DummyComponent dummyComponent) { |
| 287 | + this.belly = belly; |
| 288 | + this.dummyComponent = dummyComponent; |
| 289 | + this.autowiredCount++; |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + public void setBeanName(@NonNull String ignored) { |
| 294 | + postProcessedCount++; |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public int postProcessedCount() { |
| 299 | + return postProcessedCount; |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public int autowiredCount() { |
| 304 | + return autowiredCount; |
| 305 | + } |
| 306 | + |
| 307 | + @Override |
| 308 | + public BellyBean getBelly() { |
| 309 | + return belly; |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + public DummyComponent getDummyComponent() { |
| 314 | + return dummyComponent; |
| 315 | + } |
| 316 | + } |
| 317 | + |
191 | 318 | } |
0 commit comments