title: How to test InternalServerError using mockito in Repository?
body: <p>I am writing a test to test the POST method for failure case in the controller.
  It returns a 415, I am expecting 500. I have mocked the response using mockito.
  <strong>ControllerTest</strong></p>
  <pre><code>@Test
  @DisplayName(&quot;POST /customers - Failure&quot;)
  void createProductShouldFail() throws Exception {
      // Setup mocked service
      when(customerService.save(any(Customer.class))).thenThrow(HttpServerErrorException.InternalServerError.class);
      RequestBuilder requestBuilder = MockMvcRequestBuilders.post(&quot;/customers&quot;).accept(MediaType.APPLICATION_JSON)
              .content(&quot;{\&quot;name\&quot;:\&quot;John\&quot;}&quot;);
      MvcResult result=mockMvc.perform(requestBuilder).andReturn();
      MockHttpServletResponse response = result.getResponse();
              // Validate the response code and content type
      assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), response.getStatus());
  }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code>    @PostMapping(path = &quot;/customers&quot;)
  public ResponseEntity&lt;Customer&gt; saveCustomer(@RequestBody Customer customer){
      try {
          // Create the new product
          Customer savedCustomer = customerService.save(customer);
          // Build a created response
          return ResponseEntity
                  .created(new URI(&quot;/customers/&quot; + savedCustomer.getId()))
                  .body(savedCustomer);
      } catch (URISyntaxException e) {
          return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
      }
  }
  </code></pre>
  <p>Error:</p>
  <pre><code>      HTTP Method = POST
        Request URI = /customers
         Parameters = {}
            Headers = [Accept:&quot;application/json&quot;, Content-Length:&quot;15&quot;]
               Body = {&quot;name&quot;:&quot;John&quot;}
      Session Attrs = {}
  
  Handler:
               Type = com.prabhakar.customer.controller.CustomerController
             Method = com.prabhakar.customer.controller.CustomerController#saveCustomer(Customer)
  
  Async:
      Async started = false
       Async result = null
  
  Resolved Exception:
               Type = org.springframework.web.HttpMediaTypeNotSupportedException
  
  ModelAndView:
          View name = null
               View = null
              Model = null
  
  FlashMap:
         Attributes = null
  
  MockHttpServletResponse:
             Status = 415
      Error message = null
            Headers = [Accept:&quot;application/json, application/*+json&quot;]
       Content type = null
               Body = 
      Forwarded URL = null
     Redirected URL = null
            Cookies = []
  
  org.opentest4j.AssertionFailedError: 
  Expected :500
  Actual   :415
  </code></pre>
  <p>But 415-Unsupported Media Type client error response code.</p>
  <p>I have used the same payload for this method,it works.</p>
  <pre><code>  
  
        @Test
          @DisplayName(&quot;POST /customers - Success&quot;)
          void createProductShouldSucceed() throws Exception {
              // Setup mocked service
              Customer mockCustomer = new Customer(1L, &quot;John&quot;);
              when(customerService.save(any(Customer.class))).thenReturn(mockCustomer);
              this.mockMvc.perform(post(&quot;/customers&quot;)
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(&quot;{\&quot;name\&quot;:\&quot;John\&quot;}&quot;))
                      // Validate the response code and content type
                      .andExpect(status().isCreated())
                      .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                      //Validate returned json fields
                      .andExpect(jsonPath(&quot;$.id&quot;).value(1L))
                      .andExpect(jsonPath(&quot;$.name&quot;).value(&quot;John&quot;));
          }
  
  Update I have added
  </code></pre>
  <pre><code>@RestController
  @EnableWebMvc
  </code></pre>
  <p>this throws an error as mocked But the code breaks near <code>mockmvc.perform</code>.</p>
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException$InternalServerError
  </code></pre>
  <p>How can I verify if this is working.
  <code>assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), response.getStatus());</code></p>