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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…