简体   繁体   中英

MockMVC returned empty body

When I mock passing valid id, the test run successful, but when I pass invalid id, the returns return 404, which was expected, but there are no response, and when I normal Http Request, it returns response body with message not found and status 404. What am I doing wrong here?

@RunWith(MockitoJUnitRunner.class)
public class UserControllerTest
{
    private MockMvc mockMvc;

    @Mock
    private UserService userService;

    @InjectMocks
    private UserController userController;


    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    private ObjectMapper objectMapper;

    private User user;


    @Before
    public void setUp() 
    {

        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.setDateFormat(new ISO8601DateFormat());
        jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
        jacksonMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.valueOf("application/json")));
        jacksonMessageConverter.setObjectMapper(objectMapper);


        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders
                .standaloneSetup(userController)
                .setControllerAdvice(new ExceptionHandlerAdvice())
                .setMessageConverters(jacksonMessageConverter)
                .build();
        user = createTestUserEntity();
    }

    private User createTestUserEntity()
    {
                // create and return user block
    }

    @Test
    public void testGetReturnSuccessWhenParamIsValid() throws Exception
    {
                 // this code here returns normally json body with user object in it
    }

    @Test
    public void testGetReturnErrorWhenUserIdLessThan0() throws Exception
    {
               given(userService.getUser(anyObject())).willReturn(user);
           this.mockMvc.perform(get("/user/-1")
            .contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(print())
            .andExpect(status().isNotFound())
            .andExpect(jsonPath("$.status").value(404))
            .andExpect(jsonPath("$.message").value("not found"))
            .andReturn();
    }



In the method testGetReturnErrorWhenUserIdLessThan0(), there should be response body with status 404 and message not found, but when I test, it returns empty.

Here's the result logs:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /user/-1
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: No value at JSON path "$.status": java.lang.IllegalArgumentException: json can not be null or empty

    at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:244)
    at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:99)
    at org.springframework.test.web.servlet.result.JsonPathResultMatchers$3.match(JsonPathResultMatchers.java:124)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:164)
    at com.examaple.test.api.controllers.UserControllerTest.testGetReturnErrorWhenUserIdLessThan0(UserControllerTest.java:242)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

The response should be like this:

{
    "status": 404,
    "message": "not found"
}

Controller Class:

@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @GetMapping("/{userId:\\d+}")
    public ResourceResponse<UserView> getUser(@PathVariable("userId") String _id)
            throws ResourceNotFoundException
    {
        UserId userId;
        try {
            userId = UserId.of(_id);
        } catch (IllegalArgumentException _e) {
            throw new ResourceNotFoundException("not found");
        }
        User user = userService.getUser(userId);
        return new ResourceResponse<>(new UserView(user));
    }
}

UserId Class:

@EqualsAndHashCode
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class UserId
{

    @NonNull
    private Long userId;

    public static UserId of(@NonNull String _userId)
    {
        return User.of(Long.parseLong(_userId));
    }

    public static UserId of(@NonNull  Long _userId)
    {
        if (_userId <= 0L) {
            throw new IllegalArgumentException(String.format("Param 'userId' is not valid; Got %s", _userId));
        }
        return new UserId(_userId);
    }

    @Override
    public String toString()
    {
        return userId.toString();
    }

    public Long toLong()
    {
        return userId;
    }
}

The ID you receive in your controller method is a String, so Java will use the String method in the UserId class. As you can see, the implementation of that method will never produce a IllegalArgumentException and that is why your test is failing.

There is a lot to say about here of the code quality, but I think it is most important you understand why this code will never produce the expected test result in this case.

Your request /user/-1 doesn't match the handler mapping @GetMapping("/{userId:\\\\d+}") you think it does.

\\d is:

A digit: [0-9]

( https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM