簡體   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"

我之前在應用程序 yaml 中使用 management.port 作為管理端點端口。 但是,將其更改為在以下應用程序 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

集成測試 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\"}"));
  }

}

我認為問題是由於應用程序和執行器的不同端口而出現的。

將其更改為 0 應該可以解決您的測試問題:

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

編輯-我找到了一個相關的帖子,也許您可以查看那里提出的其他解決方案(即使沒有接受的答案): Unit testing of Spring Boot Actuator endpoints not working when specified a port

您可以使用與官方指南相同的方法。

@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