junit – 指定端口时Spring Boot Actuator端点的单元测试无效
发布时间:2020-12-15 02:58:31 所属栏目:Java 来源:网络整理
导读:最近我改变了我的 spring boot属性来定义一个管理端口. 这样做,我的单元测试开始失败:??( 我编写了一个测试/ metrics端点的单元测试,如下所示: @RunWith (SpringRunner.class)@DirtiesContext@SpringBootTestpublic class MetricsTest { @Autowired private
最近我改变了我的
spring boot属性来定义一个管理端口.
这样做,我的单元测试开始失败:??( 我编写了一个测试/ metrics端点的单元测试,如下所示: @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> 我尝试用以下方法更改@SpringBootTest注释: @SpringBootTest (properties = {"management.port=<server.port>"}) server.port使用的编号在哪里.这似乎没有任何区别. 然后将属性文件中的management.port值更改为与server.port相同.结果相同. 让测试工作的唯一方法是从属性文件中删除management.port. 有什么建议/想法吗? 谢谢 解决方法
您是否尝试将以下注释添加到测试类中?
@TestPropertySource(properties = {"management.port=0"}) Check the following link for reference. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |