The application closing context explicitly:
@Service
public class Foo {
private ConfigurableApplicationContext context;
public Foo(ConfigurableApplicationContext applicationContext) {
this.context = applicationContext;
}
public void closeContext() {
Thread thread = new Thread(() -> {
context.close();
System.out.println("Context closed");
});
thread.setDaemon(false);
thread.start();
}
}
In the test I want to check that it’s closed.
The test:
@SpringBootTest(classes = X12Application.class)
class X12ApplicationTests {
@Autowired
private Foo foo;
@Autowired
private ConfigurableApplicationContext context;
@Test
void contextLoads() throws InterruptedException {
assertFalse(context.isClosed());
foo.closeContext();
Thread.sleep(5000);
assertTrue(context.isClosed());
}
}
The asserts finish successfully .
But test ends with error:
ava.lang.IllegalStateException: The ApplicationContext loaded for [MergedContextConfiguration@c2cf597 testClass = com.example.x12.X12ApplicationTests, locations = [], classes = [com.example.x12.X12Application], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = [“org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true”], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@79da8dc5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@696da30b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@693fe6c9, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@306f16f3, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@14555e0a, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@a76cda49], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null] is not active. This may be due to one of the following reasons: 1) the context was closed programmatically by user code; 2) the context was closed during parallel test execution either according to @DirtiesContext semantics or due to automatic eviction from the ContextCache due to a maximum cache size policy.
at org.springframework.util.Assert.state(Assert.java:101)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:132)
at org.springframework.test.context.TestContext.publishEvent(TestContext.java:95)
at org.springframework.test.context.event.EventPublishingTestExecutionListener.afterTestClass(EventPublishingTestExecutionListener.java:168)
at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:538)
at org.springframework.test.context.junit.jupiter.SpringExtension.afterAll(SpringExtension.java:141)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)