@Test public void register() throws UserExistException{ User user = new User(); user.setUserName("tom"); user.setPassword("1234"); doAnswer(new Answer<User>() { public User answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); User user = (User) args[0]; if (user != null) { user.setUserId(1); } return user; } }).when(userDao).save(user); userService.register(user); assertThat(user.getUserId(), equalTo(1)); verify(userDao, times(1)).save(user); }
public void register(User user) throws UserExistException{ User u = this.getUserByUserName(user.getUserName()); if(u != null){ throw new UserExistException("用户名已经存在"); }else{ user.setCredit(100); user.setUserType(1); userDao.save(user); } }
UserService { public void register(User user) throws UserExistException{ User u = this.getUserByUserName(user.getUserName()); if(u != null){ throw new UserExistException("用户名已经存在"); }else{ user.setCredit(100); user.setUserType(1); userDao.save(user); } } }
UserService { public void register(User user) throws UserExistException{ User u = this.getUserByUserName(user.getUserName()); if(u != null){ throw new UserExistException("用户名已经存在"); }else{ user.setCredit(100); user.setUserType(1); userDao.save(user); } }  }
UserService { public void register(User user) throws UserExistException{ User u = this.getUserByUserName(user.getUserName()); if(u != null){ throw new UserExistException("用户名已经存在"); }else{ user.setCredit(100); user.setUserType(1); userDao.save(user); } }  void register(User user); void update(User user); User getUserByUserName(String userName); User getUserById(int userId); void lockUser(String userName); void unlockUser(String userName); List<User> queryUserByUserName(String userName); List<User> getAllUsers(); void loginSuccess(User user); }
UserService { public void register(User user) throws UserExistException{ User u = this.getUserByUserName(user.getUserName()); if(u != null){ throw new UserExistException("用户名已经存在"); }else{ user.setCredit(100); user.setUserType(1); userDao.save(user); } }  void register(User user); void update(User user); User getUserByUserName(String userName); User getUserById(int userId); void lockUser(String userName); void unlockUser(String userName); List<User> queryUserByUserName(String userName); List<User> getAllUsers(); void loginSuccess(User user);  }