Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing of Spring Boot Actuator endpoints not working when specifying a port

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

like image 665
WHGIBBO Avatar asked Jul 21 '16 13:07

WHGIBBO


People also ask

How do I turn on spring boot actuator endpoints?

To enable Spring Boot actuator endpoints to your Spring Boot application, we need to add the Spring Boot Starter actuator dependency in our build configuration file. Maven users can add the below dependency in your pom. xml file. Gradle users can add the below dependency in your build.

How do you change the endpoints on a spring actuator?

To create a custom actuator endpoints, Use @Endpoint annotation on a class. Then leverage @ReadOperation / @WriteOperation / @DeleteOperation annotations on the methods to expose them as actuator endpoint bean as needed.

Which actuator endpoints are enabled by default?

In order to access the actuator endpoints using HTTP, we need to both enable and expose them. By default, all endpoints but /shutdown are enabled. Only the /health and /info endpoints are exposed by default.

Which endpoint is disabled by default in spring boot actuator?

The shutdown endpoint of the spring boot actuator is disabled by default. You can enable the shutdown endpoint with the following command. The POST method can be used to run to the shutdown command.


1 Answers

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.server.port=8090
...
like image 150
MirzaM Avatar answered Sep 27 '22 22:09

MirzaM