UserServiceImpl implements UserService { @Transactional(readOnly = true) public UserDTO login(UserDTO user) throws UserNotFoundException { Assert.notNull(user); Assert.notNull(user.getName()); Assert.notNull(user.getPassword()); UserEntity userEntity; try { userEntity = userRepository.findByUserNameAndUserPassword(user.getName(), user.getPassword()); if (userEntity == null) { throw new UserNotFoundException(); } } catch (NoResultException | EmptyResultDataAccessException | NonUniqueResultException e) { throw new UserNotFoundException(e); } catch (UserNotFoundException e) { throw e; } catch (Exception e) { log.error("User failed to login", e); throw new BackendException(e); } UserDTO userDTO = converterService.convert(userEntity, UserDTO.class); return userDTO; }  @Transactional(readOnly = true) UserDTO login(UserDTO user); @Transactional @Override Long saveOrUpdate(UserDTO userDTO); @Transactional(readOnly = true) @Override UserDTO findUserByName(String username);  }
@Test(expected = UserNotFoundException.class) public void loginShouldThrowUserNotFoundExceptionBecauseOfNoResultException() throws UserNotFoundException { given(userRepository.findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD)) .willThrow(NoResultException.class); userService.login(userDTO); }
@Test(expected = UserNotFoundException.class) public void loginShouldThrowUserNotFoundExceptionBecauseOfEmptyResultDataAccessException() throws UserNotFoundException { given(userRepository.findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD)) .willThrow(EmptyResultDataAccessException.class); userService.login(userDTO); }
@Test(expected = UserNotFoundException.class) public void loginShouldThrowUserNotFoundExceptionBecauseOfNonUniqueResultException() throws UserNotFoundException { given(userRepository.findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD)) .willThrow(NonUniqueResultException.class); userService.login(userDTO); }
@Test(expected = IllegalArgumentException.class) public void loginShouldThrowIllegalArgumentExceptionBecauseOfNullParameter() throws UserNotFoundException { userService.login(NULL_USER_DTO); }
@Test(expected = IllegalArgumentException.class) public void loginShouldThrowIllegalArgumentExceptionBecauseUserDtoHasNullName() throws UserNotFoundException { UserDTO userDtoWithNullName = UserDTO.builder().name(null).password(USER_PASSWORD).build(); userService.login(userDtoWithNullName); }
@Test(expected = IllegalArgumentException.class) public void loginShouldThrowIllegalArgumentExceptionBecauseUserDtoHasNullPassword() throws UserNotFoundException { UserDTO userDtoWithNullPassword = UserDTO.builder().name(USER_NAME).password(null).build(); userService.login(userDtoWithNullPassword); }
@Test public void loginShouldReturnDetailedUserDto() throws UserNotFoundException { given(userRepository.findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD)) .willReturn(userEntity); UserDTO result = userService.login(userDTO); then(converterService).should().convert(userEntity, UserDTO.class); then(userRepository).should().findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD); assertThat(result, is(detailedUserDTO)); }
@Test(expected = UserNotFoundException.class) public void loginShouldNotReturnDetailedUserDto() throws UserNotFoundException { given(userRepository.findByUserNameAndUserPassword(USER_NAME, USER_PASSWORD)) .willReturn(NULL_USER_ENTITY); UserDTO result = userService.login(userDTO); }