title: Mocking a controller that do PUT function
body: <p>I am trying to create a Mock of my controller to update data inside user. From the code I am trying to update only the email, password, and age. Is this how we should do the mock? Because I'm still really new with this Mockito and Junit</p>
  
  <blockquote>
    <p>Controller Mock</p>
  </blockquote>
  
  <pre><code>@Test
  public void testUpdateUserController() throws Exception{
      String username = "User";
      String email = "user@email.com";
      String password = "password123";
      int age = 90;
   Mockito.when(userService.updateUser(username,email,password,age)).then(invocationOnMock -&gt; {
       User user = new User();
       user.setUsername("User");
       user.setEmail("user@user.com");
       user.setPassword("123456");
       user.setAge(12);
       user.setAddress("11111");
       user.setIsActive(true);
       return Optional.of(user);
   });
   mockMvc.perform(MockMvcRequestBuilders.put(BASE_URL + "/users/{username}",username)
           .contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.status().isOk());
   Mockito.verify(userService, Mockito.times(1)).updateUser(userCaptor.capture(),emailCaptor.capture(),passwordCaptor.capture(),ageCaptor.capture());
      Assert.assertEquals("User", userCaptor.getValue());
      Assert.assertEquals("user@email.com", emailCaptor.getValue());
      Assert.assertEquals("password123", passwordCaptor.getValue());
      Assert.assertEquals("90", ageCaptor.getValue());
      Assert.assertEquals("11111", addressCaptor.getValue());
  }
  </code></pre>
  
  <blockquote>
    <p>UserServiceImpl</p>
  </blockquote>
  
  <pre><code>@Override
  public boolean updateUser(String username, String email, String password, Integer age) {
      Optional&lt;User&gt; userList = userRepository.findByUsernameAndIsActiveTrue(username);
      if (userList.isPresent()) {
          User user = userList.get();
          user.setEmail(email);
          user.setPassword(password);
          user.setAge(age);
          userRepository.save(user);
          return true;
      } else {
          return false;
      }
  }
  </code></pre>