Avoid triggering second context creation when first attempt failed

Closes gh-24888
pull/37210/head
Andy Wilkinson 1 year ago
parent 573f01e991
commit dc45532c94

@ -49,6 +49,9 @@ public class SpringBootDependencyInjectionTestExecutionListener extends Dependen
} }
private void outputConditionEvaluationReport(TestContext testContext) { private void outputConditionEvaluationReport(TestContext testContext) {
if (!testContext.hasApplicationContext()) {
return;
}
try { try {
ApplicationContext context = testContext.getApplicationContext(); ApplicationContext context = testContext.getApplicationContext();
if (context instanceof ConfigurableApplicationContext) { if (context instanceof ConfigurableApplicationContext) {

@ -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.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
/** /**
* Tests for {@link SpringBootDependencyInjectionTestExecutionListener}. * Tests for {@link SpringBootDependencyInjectionTestExecutionListener}.
@ -59,6 +61,7 @@ class SpringBootDependencyInjectionTestExecutionListenerTests {
SpringApplication application = new SpringApplication(Config.class); SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
ConfigurableApplicationContext applicationContext = application.run(); ConfigurableApplicationContext applicationContext = application.run();
given(testContext.hasApplicationContext()).willReturn(true);
given(testContext.getApplicationContext()).willReturn(applicationContext); given(testContext.getApplicationContext()).willReturn(applicationContext);
try { try {
this.reportListener.prepareTestInstance(testContext); this.reportListener.prepareTestInstance(testContext);
@ -83,6 +86,17 @@ class SpringBootDependencyInjectionTestExecutionListenerTests {
.isEqualTo(originalFailure); .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) @Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(JacksonAutoConfiguration.class) @ImportAutoConfiguration(JacksonAutoConfiguration.class)
static class Config { static class Config {

Loading…
Cancel
Save