Spring REST mock context path

后端 未结 2 483
南旧
南旧 2021-01-19 05:25

I try to set the context path for spring rest mocks using the following code snippet:

private MockMvc mockMvc;

@Before
public void setUp() {
    this.mockMvc         


        
相关标签:
2条回答
  • 2021-01-19 05:51

    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())));
    }
    
    0 讨论(0)
  • 2021-01-19 05:55

    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())));
    }
    
    0 讨论(0)
提交回复
热议问题