Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockMvc receive 404 when prometheus endpoint is calling

I want to test Prometheus metrics endpoint using MockMvc class.

Everything works fine but yesterday I migrate my project to Java 15, SpringBoot 2.4.3 and SpringCloud 2020.0.1. Now, only prometheus test not works and I receive 404 not 200 as expected. I have all the necessary dependency on build.gradle e.q.: runtime("io.micrometer:micrometer-registry-prometheus"). On application-test.yml I have a configuration for disabled security, contract tests pact broker endpoints etc.

my test:

@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();
 }
}

Part of application.yaml configuration:

management:
  endpoint:
    prometheus:
      enabled: true
  endpoints:
    enabled-by-default: false
    web:
      exposure:
        include: 'prometheus'
      base-path: '/'
      path-mapping:
        prometheus: 'metrics'
like image 238
MatWdo Avatar asked Mar 11 '21 07:03

MatWdo


Video Answer


1 Answers

Solution: we need to add properties to the test annotation or to application-test.yml:

@ExtendWith(SpringExtension.class)
@SpringBootTest(
  webEnvironment = RANDOM_PORT,
  properties = "management.metrics.export.prometheus.enabled")
@ActiveProfiles({"test"})
@AutoConfigureMockMvc
like image 147
MatWdo Avatar answered Oct 22 '22 17:10

MatWdo