15

I'm trying to test my rest api with mockMvc.

mockMvc.perform(get("/users/1/mobile")
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(print())
        .andExpect(content().string("iPhone"))

The test failed because of:

java.lang.AssertionError: Response content 
Expected :iPhone
Actual   :

From the output of print(), I can know the API actually returned the expected string "iPhone".

ModelAndView:
        View name = users/1/mobile
             View = null
        Attribute = treeNode
            value = "iPhone"
           errors = []

And I guess the empty "Actual" above is caused by empty "Body" below

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = users/1/mobile
   Redirected URL = null
          Cookies = []

My questions are:

  1. Why MockHttpServletResponse's Body is empty;
  2. How can I correctly test the response of API.
1
  • 2
    Please add the controller you're testing (the Subject Under Test) Commented Oct 16, 2016 at 7:43

3 Answers 3

6

If your action methods (methods with @RequestMapping annotation) return instances of ModelAndView or you work with Model, you have to test it using MockMvcResultMatchers#model function:

.andExpect(MockMvcResultMatchers.model().attribute("phone", "iPhone"))
.andExpect(MockMvcResultMatchers.model().size(1))

MockMvcResultMatchers#content is appropriate for REST action methods (methods with @RequestBody annotation).


To have a better understanding about testing Spring MVC and Spring REST controllers check these links:

Sign up to request clarification or add additional context in comments.

1 Comment

but those are just the attributes of the model, is there any way to test the view render?
6

Just adding another reason for this error, that took me a whole day to discover. I successfully created an APITest using mockito and mockmvc class, using the perform method. Then copied the code to produce another service and I started to get an empty body over and over again.

Nonetheless, at the end of the day I decided to compare each copied class from one project to another. The only one difference that I found was the @EqualsAndHashCode annotation in my request DTO that is received by the new controller.

So, the recommendation is: add the @EqualsAndHashCode annotation in your DTO classes.

2 Comments

That is the true answer. Great...
Amazing. This really did the trick for me! Thank you.
0

In a testing scenario, a common issue arises when using mocking frameworks like Mockito to mock Spring Filters/Interceptors. If the filter.proceed() method isn't invoked or returns null, the subsequent web response logic isn't executed, resulting in an empty response body.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.