最近我改变了我的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());
}
}
Run Code Online (Sandbox Code Playgroud)
以前这是过去了.添加后:
management.port=9001
Run Code Online (Sandbox Code Playgroud)
测试开始失败:
home Failed: java.lang.AssertionError: Status expected: <200> but was: <404>
Run Code Online (Sandbox Code Playgroud)
我尝试用以下方法更改@SpringBootTest注释:
@SpringBootTest (properties = {"management.port=<server.port>"})
Run Code Online (Sandbox Code Playgroud)
server.port使用的编号在哪里.这似乎没有任何区别. …