title: MockMVC returned empty body
body: <p>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?</p>
  
  <pre><code>@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();
      }
  
  
  
  </code></pre>
  
  <p>In the method testGetReturnErrorWhenUserIdLessThan0(), there should be response body with status 404 and message not found, but when I test, it returns empty.</p>
  
  <p>Here's the result logs:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The response should be like this:</p>
  
  <pre><code>{
      "status": 404,
      "message": "not found"
  }
  </code></pre>
  
  <p>Controller Class:</p>
  
  <pre><code>@RestController
  @RequestMapping("/user")
  public class UserController
  {
      @Autowired
      private UserService userService;
  
      @GetMapping("/{userId:\\d+}")
      public ResourceResponse&lt;UserView&gt; 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&lt;&gt;(new UserView(user));
      }
  }
  </code></pre>
  
  <p>UserId Class:</p>
  
  <pre><code>@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 &lt;= 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;
      }
  }
  </code></pre>