简体   繁体   English

Spring 当使用“management.server.port”属性而不是弃用的“management.port”时,启动 MockMvc 测试为执行器端点提供 404

[英]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.我之前在应用程序 yaml 中使用 management.port 作为管理端点端口。 However, changed it to using as in the below application yaml file.但是,将其更改为在以下应用程序 yaml 文件中使用。 It has been failing my actuator test for the health endpoint.它一直未能通过我的健康端点执行器测试。 The endpoint works as normal when i run the application.当我运行应用程序时,端点正常工作。

application.yaml申请.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集成测试 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:将其更改为 0 应该可以解决您的测试问题:

@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编辑-我找到了一个相关的帖子,也许您可以查看那里提出的其他解决方案(即使没有接受的答案): Unit testing of Spring Boot Actuator endpoints not working when specified 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);
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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