簡體   English   中英

設置管理端口時無法使用Spring Boot的執行器

[英]Spring boot's actuator unavailable when set management port

我使用Spring Boot + Spring Security + Spring Actuator

我的JUnit測試課程:

@RunWith(SpringRunner.class)
@SpringBootTest()
@AutoConfigureMockMvc
public class ActuatorTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    @WithMockUser(roles={"USER","SUPERUSER"})
    public void getHealth() throws Exception {
        mockMvc.perform(get("/health"))
        .andExpect(status().isOk());
    }

}

可以,但是當我設置management.port: 8088 ,我的測試是KO,並顯示以下消息:

[ERROR]   ActuatorTests.getHealth:37 Status expected:<200> but was:<404>

如何在我的JUnit測試MockMvc或測試配置中設置管理端口?

management.portserver.port Spring將創建一個單獨的Web應用程序上下文和一個專用的Servlet容器,在其中注冊所有執行器。 默認的MockMvc針對主要應用程序Web上下文而不是管理上下文路由請求。 這就是您的情況-由於在主應用程序Web上下文中沒有執行器在運行,因此您會得到404。要測試在管理上下文中運行的端點,請使用以下設置:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ManagementContextMvcTest {

    @Autowired
    private ManagementContextResolver resolver;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(
                     (WebApplicationContext) resolver.getApplicationContext()).build();
    }

    @Test
    @WithMockUser(roles = { "USER", "SUPERUSER" })
    public void getHealth() throws Exception {
        mockMvc.perform(get("/health"))
           .andExpect(status().isOk());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM