简体   繁体   中英

Spring Boot MockMvc test giving 404 for actuator endpoint when using the "management.server.port" property instead of the deprecated "management.port"

I was earlier using the management.port in the application yaml for the management endpoint port. However, changed it to using as in the below application yaml file. It has been failing my actuator test for the health endpoint. The endpoint works as normal when i run the application.

application.yaml

spring:
  profiles.active: development

management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 9000

server:
  port: 8000
---
spring:
  profiles: development

logging:
  level:
    root: INFO
    org.springframework.web: INFO

---
spring:
  profiles: staging

logging:
  level:
    root: ERROR
    org.springframework.web: ERROR

Integration Test Class

package com.rps;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.rps.infrastructure.repository.PlayersInMemoryRepository;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * Integration testing the actual API Check the guide here: https://spring.io/guides/gs/testing-web/
 */
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class RPSApplicationIT {

  @Autowired
  private WebApplicationContext context;

  @Autowired
  private PlayersInMemoryRepository playersInMemoryRepository;

  private MockMvc mockMvc;

  @Before
  public void setupMockMvc() {
    mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
  }

  @Test
  public void shouldReturnActuatorHealthResponse() throws Exception {
    this.mockMvc.perform(get("/actuator/health"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().json("{\"status\":\"UP\"}"));
  }

}

I think the issues arises due to the different ports of the app and the actuator.

Changing it to 0 should do the trick for your tests:

@TestPropertySource(properties = { "management.server.port=0" })

Edit - I've found a related post, maybe you can check out other solutions proposed there (even there's no accepted answer): Unit testing of Spring Boot Actuator endpoints not working when specifying a port

You can use the same approach as the official guide says.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"management.port=0"})
public class HelloWorldApplicationTests {

  @LocalServerPort
  private int port;

  @Value("${local.management.port}")
  private int mgt;

  @Autowired
  private TestRestTemplate testRestTemplate;

  @Test
  public void shouldReturn200WhenSendingRequestToController() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
        "http://localhost:" + this.port + "/hello-world", Map.class);

    then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
  }

  @Test
  public void shouldReturn200WhenSendingRequestToManagementEndpoint() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
        "http://localhost:" + this.mgt + "/actuator", Map.class);
    then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM