问题
recently I changed my spring boot properties to define a management port. In doing so, my unit tests started to fail :(
I wrote a unit test that tested the /metrics endpoint as follows:
@RunWith (SpringRunner.class)
@DirtiesContext
@SpringBootTest
public class MetricsTest {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
/**
* Called before each test.
*/
@Before
public void setUp() {
this.context.getBean(MetricsEndpoint.class).setEnabled(true);
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
/**
* Test for home page.
*
* @throws Exception On failure.
*/
@Test
public void home()
throws Exception {
this.mvc.perform(MockMvcRequestBuilders.get("/metrics"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
Previously this was passing. After adding:
management.port=9001
The tests started failing with:
home Failed: java.lang.AssertionError: Status expected: <200> but was: <404>
I tried changing the @SpringBootTest annotation with:
@SpringBootTest (properties = {"management.port=<server.port>"})
Where is the number used for the server.port. This didn't seem to make any difference.
So then changed the management.port value in the property file to be the same as the server.port. Same result.
The only way to get the test to work is remove the management.port from the property file.
Any suggestions/thoughts ?
Thanks
回答1:
Did you try adding the following annotation to your test class?
@TestPropertySource(properties = {"management.port=0"})
Check the following link for reference.
回答2:
Had the same issue, you just have to make the management.port null by adding this in your application-test.properties (set it to empty value)
management.port=
Make sure you use the test profile in your JUnit by annotating the class with
@ActiveProfiles("test")
回答3:
Isn't there an error in the property name?
Shouldn't be
@TestPropertySource(properties = {"management.server.port=..."})
instead of @TestPropertySource(properties = {"management.port=.."})
回答4:
For Spring boot test we need to specify the port it needs to connect to.
By default, it connects to server.port
which in case of actuators is different.
This can be done by
@SpringBootTest(properties = "server.port=8090")
in application.properties
we specify the management port as below
...
management.port=8090
...
回答5:
For Spring Boot 2.x the integration tests configuration could be simplified.
For example simple custom heartbeat
endpoint
@Component
@Endpoint(id = "heartbeat")
public class HeartbeatEndpoint {
@ReadOperation
public String heartbeat() {
return "";
}
}
Where integration test for this endpoint
@SpringBootTest(
classes = HeartbeatEndpointTest.Config.class,
properties = {
"management.endpoint.heartbeat.enabled=true",
"management.endpoints.web.exposure.include=heartbeat"
})
@AutoConfigureMockMvc
@EnableAutoConfiguration
class HeartbeatEndpointTest {
private static final String ENDPOINT_PATH = "/actuator/heartbeat";
@Autowired
private MockMvc mockMvc;
@Test
void testHeartbeat() throws Exception {
mockMvc
.perform(get(ENDPOINT_PATH))
.andExpect(status().isOk())
.andExpect(content().string(""));
}
@Configuration
@Import(ProcessorTestConfig.class)
static class Config {
@Bean
public HeartbeatEndpoint heartbeatEndpoint() {
return new HeartbeatEndpoint();
}
}
}
回答6:
Try using
@SpringBootTest(properties = {"management.port="})
Properties defined in the @SpringBootTest
annotation have a higher precedence than those in application properties. "management.port="
will "unset" the management.port
property.
This way you don't have to worry about configuring the port in your tests.
来源:https://stackoverflow.com/questions/38505434/unit-testing-of-spring-boot-actuator-endpoints-not-working-when-specifying-a-por