Skip to content

Commit dc45532

Browse files
committed
Avoid triggering second context creation when first attempt failed
Closes gh-24888
1 parent 573f01e commit dc45532

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
4949
}
5050

5151
private void outputConditionEvaluationReport(TestContext testContext) {
52+
if (!testContext.hasApplicationContext()) {
53+
return;
54+
}
5255
try {
5356
ApplicationContext context = testContext.getApplicationContext();
5457
if (context instanceof ConfigurableApplicationContext) {

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import static org.assertj.core.api.Assertions.assertThat;
3535
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3636
import static org.mockito.BDDMockito.given;
37+
import static org.mockito.BDDMockito.then;
3738
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.times;
3840

3941
/**
4042
* Tests for {@link SpringBootDependencyInjectionTestExecutionListener}.
@@ -59,6 +61,7 @@ void prepareFailingTestInstanceShouldPrintReport(CapturedOutput output) throws E
5961
SpringApplication application = new SpringApplication(Config.class);
6062
application.setWebApplicationType(WebApplicationType.NONE);
6163
ConfigurableApplicationContext applicationContext = application.run();
64+
given(testContext.hasApplicationContext()).willReturn(true);
6265
given(testContext.getApplicationContext()).willReturn(applicationContext);
6366
try {
6467
this.reportListener.prepareTestInstance(testContext);
@@ -83,6 +86,17 @@ void originalFailureIsThrownWhenReportGenerationFails() {
8386
.isEqualTo(originalFailure);
8487
}
8588

89+
@Test
90+
void whenTestContextDoesNotHaveApplicationContextOnlyOneAttemptIsMadeToRetrieveIt() {
91+
TestContext testContext = mock(TestContext.class);
92+
given(testContext.getTestInstance()).willThrow(new IllegalStateException());
93+
SpringApplication application = new SpringApplication(Config.class);
94+
application.setWebApplicationType(WebApplicationType.NONE);
95+
given(testContext.hasApplicationContext()).willReturn(false);
96+
assertThatIllegalStateException().isThrownBy(() -> this.reportListener.prepareTestInstance(testContext));
97+
then(testContext).should(times(1)).getTestInstance();
98+
}
99+
86100
@Configuration(proxyBeanMethods = false)
87101
@ImportAutoConfiguration(JacksonAutoConfiguration.class)
88102
static class Config {

0 commit comments

Comments
 (0)