title: mockMvc always return 200 status - Test Exception handling with SpringBoot
body: <p><strong>ReceiptController</strong> endpoint</p>
  <pre><code>    @GetMapping(value = &quot;/{id}&quot;)
      public ReceiptOverdue getReceiptById(@PathVariable int id) {
          return receiptService.getReceipt(id);
      }
  </code></pre>
  <p><strong>ReceiptService</strong></p>
  <pre><code>public ReceiptOverdue getReceiptOverdue(Integer id) {
          return receiptRepository.findReceiptById(id).map(MGReceipt::toModel).orElseThrow(() -&gt; ReceiptNotFoundException.getException());
      }
  </code></pre>
  <p>And the <strong>ReceiptNotFoundException</strong> class</p>
  <pre><code>public class ReceiptNotFoundException extends ResponseStatusException {
  
      public ReceiptNotFoundException(HttpStatus status) {
          super(status);
      }
  
      public static ReceiptNotFoundException getException() {
          return new ReceiptNotFoundException(HttpStatus.NOT_FOUND);
      }
  }
  </code></pre>
  <p>when testing manually I get a <strong>404</strong></p>
  <pre><code>{
    &quot;timestamp&quot;: 1649076257786,
    &quot;status&quot;: 404,
    &quot;error&quot;: &quot;Not Found&quot;,
    &quot;path&quot;: &quot;/api/receipt/10&quot;
  }
  </code></pre>
  <p>But when using <strong>mockMvc</strong> in the Test case I get always 200 status</p>
  <pre><code>Expected :404
  Actual   :200
  </code></pre>
  <pre><code>@WebMvcTest(controllers = ReceiptController.class)
  class ReceiptOverdueControllerTest {
  
  
      @Autowired
      private MockMvc mockMvc;
  
      @MockBean
      private ReceiptService receiptService;
  
  
      @BeforeEach
      void setUp() {
      }
  
      @AfterEach
      void tearDown() {
      }
     
      @Test 
      void whenReceiptNotFoundReturn404() throws Exception {
          var mvcResult = mockMvc.perform(get(&quot;/api/receipt/10&quot;))
                  .andDo(print())
                  .andExpect(status().isNotFound());
      }   
  } 
  </code></pre>
  <p><strong>BUT</strong> if I throw the exception in the controller the test will pass ! Is there an issue on how I throw the exception in the service ? or the problem is in my mockMvc Test ? Thanks</p>