@Test(expected = DuplicateRecordException.class) public void saveOrUpdateShouldThrowDuplicateRecordExceptionBecauseOfConstraintViolationException() { given(userRepository.saveAndFlush(userEntity)).willThrow(ConstraintViolationException.class); userService.saveOrUpdate(detailedUserDTO); }
@Transactional @Override public Long saveOrUpdate(UserDTO userDTO) { Assert.notNull(userDTO); UserEntity entity = converterService.convert(userDTO, UserEntity.class); try { return userRepository.saveAndFlush(entity).getId(); } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userName = entity.getUserName(); throw new DuplicateRecordException(userName, "Duplication occurred when saving user: " + userName, ex); } }
UserServiceImpl implements UserService { @Transactional @Override public Long saveOrUpdate(UserDTO userDTO) { Assert.notNull(userDTO); UserEntity entity = converterService.convert(userDTO, UserEntity.class); try { return userRepository.saveAndFlush(entity).getId(); } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userName = entity.getUserName(); throw new DuplicateRecordException(userName, "Duplication occurred when saving user: " + userName, ex); } } }
UserServiceImpl implements UserService { @Transactional @Override public Long saveOrUpdate(UserDTO userDTO) { Assert.notNull(userDTO); UserEntity entity = converterService.convert(userDTO, UserEntity.class); try { return userRepository.saveAndFlush(entity).getId(); } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userName = entity.getUserName(); throw new DuplicateRecordException(userName, "Duplication occurred when saving user: " + userName, ex); } }  }
UserServiceImpl implements UserService { @Transactional @Override public Long saveOrUpdate(UserDTO userDTO) { Assert.notNull(userDTO); UserEntity entity = converterService.convert(userDTO, UserEntity.class); try { return userRepository.saveAndFlush(entity).getId(); } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userName = entity.getUserName(); throw new DuplicateRecordException(userName, "Duplication occurred when saving user: " + userName, ex); } }  @Transactional(readOnly = true) UserDTO login(UserDTO user); @Transactional @Override Long saveOrUpdate(UserDTO userDTO); @Transactional(readOnly = true) @Override UserDTO findUserByName(String username); }
UserServiceImpl implements UserService { @Transactional @Override public Long saveOrUpdate(UserDTO userDTO) { Assert.notNull(userDTO); UserEntity entity = converterService.convert(userDTO, UserEntity.class); try { return userRepository.saveAndFlush(entity).getId(); } catch (ConstraintViolationException | DataIntegrityViolationException ex) { log.error("Save to repository failed.", ex); String userName = entity.getUserName(); throw new DuplicateRecordException(userName, "Duplication occurred when saving user: " + userName, ex); } }  @Transactional(readOnly = true) UserDTO login(UserDTO user); @Transactional @Override Long saveOrUpdate(UserDTO userDTO); @Transactional(readOnly = true) @Override UserDTO findUserByName(String username);  }