title: How do i unit test an exception that's being thrown by an "update user" endpoint?
body: <p>I'm currently unit testing the endpoints of a <strong>Spring Boot CRUD RESTful API</strong> and i have the following <em>&quot;Update user by its id&quot;</em> endpoint that's composed of a <strong>controller</strong> and a <strong>service</strong> to implement its logic :</p>
  <p><strong>Update controller</strong> (it's mainly calling the logic of the service and defining some guidelines) :</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/users/{id}&quot;)
  public class UpdateUserController {
  
      @Autowired
      UpdateUserService service;
  
      @PutMapping
      @ResponseStatus(HttpStatus.OK)
      public ResponseEntity&lt;User&gt; updateUser_whenPutUser(@RequestBody User user, @PathVariable Long id) {
          return ResponseEntity.ok().body(service.updateUser(id, user));
      }
  }
  </code></pre>
  <p><strong>Update service</strong> :</p>
  <pre><code>@Service
  public class UpdateUserService {
  
      @Autowired
      UserRepository repository;
  
      public User updateUser(Long id, User user) {
  
          repository.findById(id)
                  .orElseThrow(() -&gt; new UserNotFoundException(id));
  
          user.setId(id);
          return repository.save(user);
      }
  }
  </code></pre>
  <p>While everything seems to be working fine until now, i'd appreciate if someone could tell me what i could improve in the code above. Anyways, my controllers throw exception unit test is the problem i can't seem to solve :</p>
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(UpdateUserController.class)
  public class UpdateUserControllerTest {
      @Autowired
      private MockMvc mvc;
  
      @MockBean
      private UpdateUserService updateUserService;
  
      @Test
      public void updateUser_whenPutUser() throws Exception {
  
          User user = new User();
          user.setName(&quot;Test Name&quot;);
          user.setId(89L);
  
          given(updateUserService.updateUser(user.getId(), user)).willReturn(user);
  
          ObjectMapper mapper = new ObjectMapper();
  
          mvc.perform(put(&quot;/users/&quot; + user.getId().toString())
                  .content(mapper.writeValueAsString(user))
                  .contentType(MediaType.APPLICATION_JSON))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath(&quot;name&quot;, is(user.getName())));
      }
  
      @Test
      public void should_throw_exception_when_user_doesnt_exist() throws Exception {
          User user = new User();
          user.setId(89L);
          user.setName(&quot;Test Name&quot;);
  
          Mockito.doThrow(new UserNotFoundException(user.getId())).when(updateUserService).updateUser(user.getId(), user);
  
          mvc.perform(put(&quot;/users/&quot; + user.getId().toString())
                  .contentType(MediaType.APPLICATION_JSON))
                  .andExpect(status().isNotFound());
      }
  }
  </code></pre>
  <p>Although the <em>updateUser_whenPutUser</em> is passing, the exceptions unit test is resulting in :</p>
  <pre><code>java.lang.AssertionError: Status expected:&lt;404&gt; but was:&lt;400&gt;
  Expected :404
  Actual   :400
  </code></pre>
  <p>Which means it's getting a <strong>400</strong> response status instead of the expected <strong>404</strong> thrown by the exception.</p>
  <p>Curiously, if i change my <em>@WebMvcTest(UpdateUserController.class)</em> to <em>@WebMvcTest(UpdateUserService.class)</em> it passes, but then my controllers main unit test fails. No idea why.</p>
  <p>Would appreciate if someone could help with this one.</p>