PasswordChangeServiceImpl implements PasswordChangeService { @Transactional @Override public void deleteOldestPassword(Long userId) { Assert.notNull(userId); PasswordHistoryEntity entity = passwordHistoryRepository.findTop1ByUserEntityIdOrderByChangeDate(userId); passwordHistoryRepository.delete(entity); }  @Transactional @Override Long saveOrUpdate(PasswordHistoryDTO passwordHistoryDTO); @Transactional(readOnly = true) @Override List<String> getOldPasswords(Long userId); @Transactional @Override void deleteOldestPassword(Long userId);  }
@Test(expected = IllegalArgumentException.class) public void deleteOldestPasswordShouldThrowIAEWhenUserIdIsNull() { this.passwordChangeService.deleteOldestPassword(null); }
@Test(expected = DataAccessException.class) public void deleteOldestPasswordShouldDelegateDataAccessExceptionWhenQueryingOldestPassword() { given(this.passwordHistoryRepository.findTop1ByUserEntityIdOrderByChangeDate(USER_ID)) .willThrow(DataIntegrityViolationException.class); this.passwordChangeService.deleteOldestPassword(USER_ID); }
@Test(expected = DataAccessException.class) public void deleteOldestPasswordShouldDelegateDataAccessExceptionWhenDeletingHistoryEntity() { given(this.passwordHistoryRepository.findTop1ByUserEntityIdOrderByChangeDate(USER_ID)) .willReturn(EMPTY_PASSWORD_HISTORY_ENTITY); willThrow(DataIntegrityViolationException.class).given(this.passwordHistoryRepository) .delete(EMPTY_PASSWORD_HISTORY_ENTITY); this.passwordChangeService.deleteOldestPassword(USER_ID); }
@Test public void deleteOldestPasswordShouldDeleteHistoryEntity() { given(this.passwordHistoryRepository.findTop1ByUserEntityIdOrderByChangeDate(USER_ID)) .willReturn(EMPTY_PASSWORD_HISTORY_ENTITY); this.passwordChangeService.deleteOldestPassword(USER_ID); then(this.passwordHistoryRepository).should().findTop1ByUserEntityIdOrderByChangeDate(USER_ID); then(this.passwordHistoryRepository).should().delete(EMPTY_PASSWORD_HISTORY_ENTITY); verifyNoMoreInteractions(this.passwordHistoryRepository); verifyZeroInteractions(this.userRepository); }