最近我改变了我的
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
测试开始失败:
我尝试用以下方法更改@SpringBootTest注释:
- @SpringBootTest (properties = {"management.port=<server.port>"})
server.port使用的编号在哪里.这似乎没有任何区别.
然后将属性文件中的management.port值更改为与server.port相同.结果相同.
让测试工作的唯一方法是从属性文件中删除management.port.
有什么建议/想法吗?
谢谢
解决方法
您是否尝试将以下注释添加到测试类中?
- @TestPropertySource(properties = {"management.port=0"})