Spring Boot returning wrong Status Code only in Unit Test

后端 未结 1 1091
孤城傲影
孤城傲影 2021-01-18 04:17

I\'m developing a REST API with Spring Boot.

I have a controller to create a new user, that responds with 201 (CREATED) when the user is created. The response has no

相关标签:
1条回答
  • 2021-01-18 05:13

    The problem was because my controller and service are async, so my unit test it's not waiting for the correct response.

    Changed my unit test to:

    MvcResult result = mvc.perform(post("/api/users/new")
      .content(TestHelpers.convertToJson(registroMock))
      .contentType(TestHelpers.getJsonMediaType()))
      .andReturn();
    
    mvc.perform(asyncDispatch(result))
       .andExpect(status().isCreated());
    
    0 讨论(0)
提交回复
热议问题