Java 指定端口时,Spring Boot执行器端点的单元测试不起作用
最近,我更改了spring引导属性以定义一个管理端口。 在这样做的过程中,我的单元测试开始失败:( 我编写了一个单元测试,测试/metrics端点,如下所示:Java 指定端口时,Spring Boot执行器端点的单元测试不起作用,java,spring-boot,junit,spring-boot-actuator,Java,Spring Boot,Junit,Spring Boot Actuator,最近,我更改了spring引导属性以定义一个管理端口。 在这样做的过程中,我的单元测试开始失败:( 我编写了一个单元测试,测试/metrics端点,如下所示: @RunWith (SpringRunner.class) @DirtiesContext @SpringBootTest public class MetricsTest { @Autowired private WebApplicationContext context; private MockMvc m
@RunWith (SpringRunner.class)
@DirtiesContext
@SpringBootTest
public class MetricsTest {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
/**
* Called before each test.
*/
@Before
public void setUp() {
this.context.getBean(MetricsEndpoint.class).setEnabled(true);
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
/**
* Test for home page.
*
* @throws Exception On failure.
*/
@Test
public void home()
throws Exception {
this.mvc.perform(MockMvcRequestBuilders.get("/metrics"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
以前这是通过的。添加后:
management.port=9001
测试开始时失败,原因是:
home Failed: java.lang.AssertionError: Status expected: <200> but was: <404>
home失败:java.lang.AssertionError:应为状态:但为:
我尝试使用以下内容更改@SpringBootTest注释:
@SpringBootTest (properties = {"management.port=<server.port>"})
@SpringBootTest(属性={”management.port=“})
其中是用于server.port的数字。这似乎没有任何区别
因此,将属性文件中的management.port值更改为与server.port相同。结果相同
使测试工作的唯一方法是从属性文件中删除management.port
有什么建议/想法吗
谢谢尝试使用
@SpringBootTest(properties = {"management.port="})
@springbootest
注释中定义的属性比应用程序属性中定义的属性具有更高的优先级。管理.port=“
将“取消”管理.port属性。这样,您就不必担心在测试中配置端口。是否尝试将以下注释添加到测试类中
@TestPropertySource(properties = {"management.port=0"})
.也有同样的问题,您只需将management.port设置为null,方法是将其添加到application-test.properties中(将其设置为空值) 通过使用注释类来确保在JUnit中使用测试概要文件
@ActiveProfiles("test")
对于Spring引导测试,我们需要指定它需要连接的端口 默认情况下,它连接到服务器端口,在执行器的情况下,该端口不同 这可以通过以下方式完成:
@SpringBootTest(properties = "server.port=8090")
在
application.properties
中,我们指定管理端口如下
...
management.port=8090
...
物业名称中没有错误吗? 不应该
@TestPropertySource(properties={“management.server.port=…”})
而不是@TestPropertySource(properties={“management.port=…”})
对于Spring Boot 2.x,集成测试配置可以简化
例如,简单自定义<代码>心跳代码>端点
@Component
@Endpoint(id = "heartbeat")
public class HeartbeatEndpoint {
@ReadOperation
public String heartbeat() {
return "";
}
}
其中,此端点的集成测试
@SpringBootTest(
classes = HeartbeatEndpointTest.Config.class,
properties = {
"management.endpoint.heartbeat.enabled=true",
"management.endpoints.web.exposure.include=heartbeat"
})
@AutoConfigureMockMvc
@EnableAutoConfiguration
class HeartbeatEndpointTest {
private static final String ENDPOINT_PATH = "/actuator/heartbeat";
@Autowired
private MockMvc mockMvc;
@Test
void testHeartbeat() throws Exception {
mockMvc
.perform(get(ENDPOINT_PATH))
.andExpect(status().isOk())
.andExpect(content().string(""));
}
@Configuration
@Import(ProcessorTestConfig.class)
static class Config {
@Bean
public HeartbeatEndpoint heartbeatEndpoint() {
return new HeartbeatEndpoint();
}
}
}
因此,为成功和失败案例添加一些跟踪日志记录。在成功案例中,我看到以下内容:
TRACE[Test worker]--org.springframework.Test.web.servlet.TestDispatcherServlet:Testing handler map[org.springframework.boot.actuate.endpoint.mvc。EndpointHandlerMapping@2281521a]在名为
的DispatcherServlet中,失败的情况下缺少此项。因此,我只能假设我对MockMvcBuilders.webAppContextSetup(This.context)
使用了错误的值。请您对您的答案添加一些解释,谢谢分享。我如何添加负面的用例?
@SpringBootTest(
classes = HeartbeatEndpointTest.Config.class,
properties = {
"management.endpoint.heartbeat.enabled=true",
"management.endpoints.web.exposure.include=heartbeat"
})
@AutoConfigureMockMvc
@EnableAutoConfiguration
class HeartbeatEndpointTest {
private static final String ENDPOINT_PATH = "/actuator/heartbeat";
@Autowired
private MockMvc mockMvc;
@Test
void testHeartbeat() throws Exception {
mockMvc
.perform(get(ENDPOINT_PATH))
.andExpect(status().isOk())
.andExpect(content().string(""));
}
@Configuration
@Import(ProcessorTestConfig.class)
static class Config {
@Bean
public HeartbeatEndpoint heartbeatEndpoint() {
return new HeartbeatEndpoint();
}
}
}