title: How to mock http POST with applicationType Json with Mockito Java
body: <p>I want to mock http POST with json data.</p>
  
  <p>For GET method I succedded with the following code:</p>
  
  <pre><code>import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
  
  when(request.getMethod()).thenReturn("GET");
  when(request.getPathInfo()).thenReturn("/getUserApps");
  when(request.getParameter("userGAID")).thenReturn("test");
  when(request.getHeader("userId")).thenReturn("xxx@aaa-app.com");
  </code></pre>
  
  <p>My problem is with http POST request body. I want it to contain <code>application/json</code> type content.</p>
  
  <p>Something like this, but what should be the request params to answer json response?</p>
  
  <pre><code>HttpServletRequest request = mock(HttpServletRequest.class);
  HttpServletResponse response = mock(HttpServletResponse.class);
  
  when(request.getMethod()).thenReturn("POST");
  when(request.getPathInfo()).thenReturn("/insertPaymentRequest");
  when( ????  ).then( ???? maybe ?? // new Answer&lt;Object&gt;() {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
          new Gson().toJson("{id:213213213 , amount:222}", PaymentRequest.class);
          }
      });
  </code></pre>
  
  <p>Or maybe "public Object answer..." is not the correct method to use for Json return.</p>
  
  <pre><code>usersServlet.service(request, response);
  </code></pre>