From dc45532c940692aefb58ac41c779d37a5f797e98 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 10 Aug 2023 14:39:28 +0100 Subject: [PATCH] Avoid triggering second context creation when first attempt failed Closes gh-24888 --- ...otDependencyInjectionTestExecutionListener.java | 3 +++ ...endencyInjectionTestExecutionListenerTests.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java index 2a9f6afdd9..6f623c0630 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java @@ -49,6 +49,9 @@ public class SpringBootDependencyInjectionTestExecutionListener extends Dependen } private void outputConditionEvaluationReport(TestContext testContext) { + if (!testContext.hasApplicationContext()) { + return; + } try { ApplicationContext context = testContext.getApplicationContext(); if (context instanceof ConfigurableApplicationContext) { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java index a23afc2c8f..c01d39632d 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java @@ -34,7 +34,9 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; /** * Tests for {@link SpringBootDependencyInjectionTestExecutionListener}. @@ -59,6 +61,7 @@ class SpringBootDependencyInjectionTestExecutionListenerTests { SpringApplication application = new SpringApplication(Config.class); application.setWebApplicationType(WebApplicationType.NONE); ConfigurableApplicationContext applicationContext = application.run(); + given(testContext.hasApplicationContext()).willReturn(true); given(testContext.getApplicationContext()).willReturn(applicationContext); try { this.reportListener.prepareTestInstance(testContext); @@ -83,6 +86,17 @@ class SpringBootDependencyInjectionTestExecutionListenerTests { .isEqualTo(originalFailure); } + @Test + void whenTestContextDoesNotHaveApplicationContextOnlyOneAttemptIsMadeToRetrieveIt() { + TestContext testContext = mock(TestContext.class); + given(testContext.getTestInstance()).willThrow(new IllegalStateException()); + SpringApplication application = new SpringApplication(Config.class); + application.setWebApplicationType(WebApplicationType.NONE); + given(testContext.hasApplicationContext()).willReturn(false); + assertThatIllegalStateException().isThrownBy(() -> this.reportListener.prepareTestInstance(testContext)); + then(testContext).should(times(1)).getTestInstance(); + } + @Configuration(proxyBeanMethods = false) @ImportAutoConfiguration(JacksonAutoConfiguration.class) static class Config {