Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

Testing Spring Boot Actuator endpoint without starting full application

My Spring Boot application is configured with a datasource and exposes the Spring Actuator health and prometheus metrics.

application.yml

spring:
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    username: ${db.username}
    password: ${db.password}
    url: jdbc:mariadb://${db.host}:${db.port}/${db.schema}

management:
  endpoints:
    web:
      exposure:
        include: 'health, prometheus'

When starting the application, /actuator/prometheus delivers a response containing metrics. Now I'd like to write a very basic test (JUnit 5) for the prometheus endpoint. This is how it looks at the moment:

Test Class

@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class HealthMetricsIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldProvideHealthMetric() throws Exception {
        mockMvc.perform(get("/actuator/prometheus")).andExpect(status().isOk());
    }
}

However I'm running into two issues here and I don't know yet how to solve them.

Issue 1

  • With this setup, the test seems to bring up the whole application, thus trying to connect to a running database.
  • The test won't start properly because the datasource properties prefixed with db are not provided.

How can I start this test without bringing up the database connection?

Issue 2

Even if my local database is running and I provide all db properties, the test fails. This time because I get a HTTP 404 instead of a 200.

question from:https://stackoverflow.com/questions/65903852/testing-spring-boot-actuator-endpoint-without-starting-full-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As MockMvc is for testing Spring MVC components (your @Controller and @RestController) I guess the auto-configured mocked Servlet environment you get with @AutoConfigureMockMvc won't contain any Actuator endpoints.

Instead, you can write an integration test that doesn't use MockMvc and rather starts your embedded Servlet container.

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
// @ExtendWith(SpringExtension.class) can be omitted with recent Spring Boot versions
public class HealthMetricsIT {

    @Autowired
    private WebTestClient webTestClient; // or TestRestTemplate

    @Test
    public void shouldProvideHealthMetric() throws Exception {
      webTestClient
       .get()
       .uri("/actuator/health")
       .exchange()
       .expectStatus().isOk();
    }
}

For this test, you have to make sure that all your infrastructure components (database, etc.) are available that are required upon application startup.

Using Testcontainers, you can provide a database for your integration test with almost no effort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...