title: How could I test update method use Junit
body: <pre><code>public boolean changePassword(User user, String newpass) {
      user.setPasswordHash(newpass);
      if(update(user) == false)
          return false;
      return true;
  }
  
  public boolean update(User user){
     SqlSession session = factory.openSession()
     try {
           UserMapper mapper = session.getMapper(UserMapper.class);
              mapper.update(user);
              session.commit();
              return  true;
          }catch (Exception e){
              e.getMessage();
              return false;
          }
          finally {
              session.close();
          }
  }
  </code></pre>
  
  <p>now this is one of my test.it is when change Pasword Success.</p>
  
  <pre><code>  @Test
  public void shouldChangeUserPasswordWhenAsked() {
      User  updatedUser = new User();
      updatedUser.setUserName("user");
      updatedUser.setPasswordHash("newpassword");
      when(userMapper.findByUsername("user")).thenReturn(updatedUser);
  
      Boolean updateResult = userService.password(user,"newpassword");
      verify(userMapper).update(user);
      assertEquals("updatedUserSuccess",userService.getByUsername(user.getUserName()).getPasswordHash(),"newpassword");
      assertEquals("updatedSuccess",updateResult,true);
  }
  </code></pre>
  
  <p>now how I want to test the failed situation,how can I do it? should I change my update method? before test I mock dabase and some method rely on database like this</p>
  
  <pre><code> @Before
  public void setup(){
      session = mock(SqlSession.class);
      userMapper = mock(UserMapper.class);
      userRoleMapper = mock(UserRoleMapper.class);
  
      sessionFactory = mock(SqlSessionFactory.class);
  
      when(sessionFactory.openSession()).thenReturn(session);
      when(session.getMapper(UserMapper.class)).thenReturn(userMapper);
      when(session.getMapper(UserRoleMapper.class)).thenReturn(userRoleMapper);
  
      userService = new UserServiceImpl(sessionFactory);
  
      user = new User();
      user.setUserName("user");
      user.setPasswordHash("password");
  }
  </code></pre>