Allow ApplicationContextRunner to return non-refreshed context

Add `AbstractApplicationContextRunner.prepare` which can be used to
test an `ApplicationContext` that has been prepared but not refreshed.

Closes gh-31302
This commit is contained in:
Phillip Webb
2022-06-08 18:53:09 -07:00
parent 46c262d3cc
commit 11184aae8d
2 changed files with 42 additions and 13 deletions

View File

@@ -47,6 +47,7 @@ import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Abstract tests for {@link AbstractApplicationContextRunner} implementations.
@@ -238,6 +239,16 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
.run((context) -> assertThat(context).hasSingleBean(ProfileConfig.class));
}
@Test
void prepareDoesNotRefreshContext() {
get().withUserConfiguration(FooConfig.class).prepare((context) -> {
assertThatIllegalStateException().isThrownBy(() -> context.getBean(String.class))
.withMessageContaining("not been refreshed");
context.getSourceApplicationContext().refresh();
assertThat(context.getBean(String.class)).isEqualTo("foo");
});
}
protected abstract T get();
private static void throwCheckedException(String message) throws IOException {