Spring REST mock context path

﹥>﹥吖頭↗ 提交于 2019-12-01 22:18:43

You need to include the context path in the path that you're passing to get.

In the case you've shown in the question, the context path is /api and you want to make a request to / so you need to pass /api/ to get:

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/api/").contextPath("/api").accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}

What many people do is simply not use the context path in mockMvc tests. You only specify the @RequestMapping URI:

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/business-case/1234").accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!