UserDAO { public void update(User user) throws ValidationException { saveUser(user); } @Autowired  UserDAO(UserRepository userRepository, FileDAO fileDAO, ProjectDAO projectDAO,
                   ProjectRepository projectRepository); void create(User newUser); List<User> getAll(); List<User> getAllByRole(UserRole role); User getById(Long id); User getByEmail(String email); User getByUsername(String username); void update(User user); void delete(User authUser, Long id); void delete(User authUser, List<Long> userIds);  }
@Test public void shouldUpdateAUser() throws NotFoundException { User user = createUser(); userDAO.update(user); Mockito.verify(userRepository).save(user); }
@Test(expected = ValidationException.class) public void shouldHandleTransactionSystemExceptionsOnUserUpdateGracefully() { User user = createUser(); ConstraintViolationException constraintViolationException; constraintViolationException = new ConstraintViolationException("User is not valid!", new HashSet<>()); RollbackException rollbackException = new RollbackException("RollbackException", constraintViolationException); TransactionSystemException transactionSystemException; transactionSystemException = new TransactionSystemException("Spring TransactionSystemException", rollbackException); BDDMockito.given(userRepository.save(user)).willThrow(transactionSystemException); userDAO.update(user); }
@Test(expected = ValidationException.class) public void shouldHandleConstraintViolationExceptionOnUserUpdateGracefully() { User user = createUser(); BDDMockito.given(userRepository.save(user)).willThrow(ConstraintViolationException.class); userDAO.update(user); }
@Test(expected = ValidationException.class) public void shouldHandleDataIntegrityViolationExceptionOnUserUpdateGracefully() { User user = createUser(); BDDMockito.given(userRepository.save(user)).willThrow(DataIntegrityViolationException.class); userDAO.update(user); }