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

前端 未结 6 1903
忘了有多久
忘了有多久 2021-01-04 06:43

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 /

6条回答
  •  既然无缘
    2021-01-04 07:26

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

提交回复
热议问题