【发布时间】:2020-07-05 11:13:58
【问题描述】:
我想为自定义的 HealthIndicator 编写一个简单的测试,它在它自己的模块中定义,与 Spring 应用程序的模块分开。 HealthIndicator 非常简单:
@Component
public class MyHealthIndicator implements HealthIndicator {
public Health health() {
return Health.up().withDetail("my-health", "okay").build();
}
}
这是测试:
@WebAppConfiguration
@ContextConfiguration(classes = {TestConfiguration.class, MyHealthIndicator.class})
@ExtendWith(SpringExtension.class)
class MyHealthIndicatorTests {
@Autowired
private MockMvc mockMvc;
@Test
public void healthTest() throws Exception {
mockMvc.perform(get("/management/health")).andExpect(status().isOk());
}
}
这是简单的测试配置:
@Configuration
@EnableWebMvc
@ImportAutoConfiguration(classes = {HealthEndpointAutoConfiguration.class, MockMvcAutoConfiguration.class})
@TestPropertySource(properties = {"debug:true", "management.endpoints.enabled-by-default:true",
"management.endpoints.web.base-path:/management", "management.endpoints.web.exposure.include=health"})
public class TestConfiguration {
}
健康端点的测试返回 404 - Not Found。
在日志中,我看到 TestDispatcherServlet 没有健康端点的映射:
12:48:38.309 [main] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /management/health
但是为什么?
在日志中,我还看到实际创建了 HealthEndpoint:
12:48:38.013 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'healthEndpoint'
但 TestDispatcherServlet 中的映射似乎没有配置。如何启用映射?
请注意:我想在没有 SpringBootApplication 的情况下测试我的健康指标。这根本不可能吗?
编辑: POM 没有什么特别之处:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
标签: spring-boot spring-mvc spring-boot-actuator