相关疑难解决方法(0)

指定端口时,Spring Boot Actuator端点的单元测试无效

最近我改变了我的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使用的编号在哪里.这似乎没有任何区别. …

junit spring-boot spring-boot-actuator

9
推荐指数
2
解决办法
5625
查看次数

标签 统计

junit ×1

spring-boot ×1

spring-boot-actuator ×1