我想使用MockMvc
类来测试Prometheus度量终端。
一切都很顺利,但昨天我将我的项目迁移到了Java 15
、SpringBoot 2.4.3
和SpringCloud 2020.0.1
。现在,只有Prometheus测试不能正常工作,我收到的是 404 而不是预期的200。 我在build.gradle
中拥有所有必要的依赖项,例如:runtime("io.micrometer:micrometer-registry-prometheus")
。在application-test.yml
中,我对禁用安全性、合同测试协议代理端点等进行了配置。
我的测试代码:
@ExtendWith({SpringExtension.class, PostgresSpringDataSourceExtension.class})
@ActiveProfiles({"test"})
@SpringBootTest
@AutoConfigureMockMvc
public class PrometheusEndpointTest {
@Autowired private MockMvc mockMvc;
@Test
public void metricsThroughPrometheusEndpoint() throws Exception {
MvcResult result =
this.mockMvc.perform(get("/metrics")).andExpect(status().isOk()).andReturn();
}
}
application.yaml
配置文件的一部分:
management:
endpoint:
prometheus:
enabled: true
endpoints:
enabled-by-default: false
web:
exposure:
include: 'prometheus'
base-path: '/'
path-mapping:
prometheus: 'metrics'