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) {
if (!testContext.hasApplicationContext()) {
return;
}
try {
ApplicationContext context = testContext.getApplicationContext();
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.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 {

Loading…
Cancel
Save