[英]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.port
与server.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.