Моя цель - проверить вызов пружинного привода для завершения работы моего приложения.
Я написал следующий тест:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyAppTest {
private static final String SHUTDOWN_URL = "/actuator/shutdown";
@Autowired
protected TestRestTemplate restTemplate;
@Autowired
protected ConfigurableApplicationContext ctx;
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void applicationShutsDown() throws URISyntaxException {
RequestEntity<Void> request = RequestEntity.post(new URI(SHUTDOWN_URL)).contentType(MediaType.APPLICATION_JSON).build();
ResponseEntity<Void> response = restTemplate.exchange(request, Void.class);
assertThat(response.getStatusCode()).isSameAs(HttpStatus.OK);
await().atMost(Duration.ofSeconds(30))
.until(() -> !ctx.isActive() && !ctx.isRunning());
}
}
Я вижу, что этот тест проходит нормально. Но после выполнения теста я получаю ошибку ниже.
В результате тест помечается как неудачный, несмотря на то, что все утверждения выполнены успешно.
java.lang.IllegalStateException: The ApplicationContext loaded for [[WebMergedContextConfiguration@eda25e5 testClass = MyAppTest, locations = '{}', classes = '{class com.mytest.MyAppTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4bdeaabb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@71075444, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5745ca0e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4d15107f, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath = 'src/main/webapp', 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:97)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
at org.springframework.test.context.TestContext.publishEvent(TestContext.java:94)
at org.springframework.test.context.event.EventPublishingTestExecutionListener.afterTestExecution(EventPublishingTestExecutionListener.java:129)
at org.springframework.test.context.TestContextManager.afterTestExecution(TestContextManager.java:379)
at org.springframework.test.context.junit.jupiter.SpringExtension.afterTestExecution(SpringExtension.java:129)