简体   繁体   English

设置管理端口时无法使用Spring Boot的执行器

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

I use Spring boot + Spring Security + Spring Actuator 我使用Spring Boot + Spring Security + Spring Actuator

My JUnit test class: 我的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());
    }

}

is OK, but when I set management.port: 8088 , my test is KO with this message: 可以,但是当我设置management.port: 8088 ,我的测试是KO,并显示以下消息:

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

How to set management port in my JUnit test MockMvc or test configuration? 如何在我的JUnit测试MockMvc或测试配置中设置管理端口?

When management.port is different to server.port Spring will create a separate web application context and a dedicated servlet container where it will register all actuators. management.portserver.port Spring将创建一个单独的Web应用程序上下文和一个专用的Servlet容器,在其中注册所有执行器。 A default MockMvc routes requests against the main application web context and not the management one. 默认的MockMvc针对主要应用程序Web上下文而不是管理上下文路由请求。 That is what happening in your case - since no actuators are running in the main application web context you get a 404. To test endpoints running in a management context use the following setup: 这就是您的情况-由于在主应用程序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.

相关问题 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" 测试期间弹簧靴的执行器端点不可用 - Spring boot's actuator end points are unavailable during test Spring Boot的健康执行器 - 什么时候启动? - Spring Boot's health actuator - WHEN is it UP? Spring 引导执行器端点的单元测试在指定端口时不起作用 - Unit testing of Spring Boot Actuator endpoints not working when specifying a port 无法保护Spring启动管理执行器端点 - Unable to secure Spring boot management actuator endpoints Spring Boot执行器管理端点异常处理 - Spring Boot Actuator Management Endpoint Exception Handling Spring 启动执行器未从指定端口启动 - Spring boot actuator not start with the port specified 用于 Spring MVC 的 Spring Boot Actuator,无需在另一个端口上启动 Spring - Spring Boot Actuator for Spring MVC without Spring boot on another port Java Spring 启动 - 将执行器健康端点的端口更改为自定义端口 - Java Spring Boot - Change the Port of Actuator Health Endpoint to a Custom Port Spring 启动执行器记录器 - Spring boot actuator loggers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM