UserService { public User findUser(Integer id) { logger.debug("user.findAccount: id=" + id); Optional<User> account = user.findById(id); if (!account.isPresent()) { logger.warn("UserService.findAccount: could not find account with id: " + id); throw new NoRecordsFoundException(); } logger.info(String .format("UserService.findAccount - retrieved account with id: %s. Payload is: %s", id, account.get())); return account.get(); }  User findUser(Integer id); User findUser(String username); @Cacheable(value = "authorizationCache") User findAccountprofileByAuthtoken(String token); Integer saveUser(User userRequest); Map<String, Object> login(String username, String password); User logout(String userId);  }
@Test public void doFindUser() { when(repo.findById(ServiceTestConfiguration.PROFILE_ID)).thenReturn(Optional.of(ServiceTestConfiguration.user())); assertEquals(service.findUser(ServiceTestConfiguration.PROFILE_ID).toString(),ServiceTestConfiguration.user().toString()); }
@Test public void doFindAccountUserId() { when(repo.findByUserid(ServiceTestConfiguration.USER_ID)).thenReturn(ServiceTestConfiguration.user()); assertEquals(service.findUser(ServiceTestConfiguration.USER_ID).toString(),ServiceTestConfiguration.user().toString()); }
@Test(expected=NoRecordsFoundException.class) public void doFindAccountUserIdNotFound() { when(repo.findByUserid(ServiceTestConfiguration.BAD_USER_ID)).thenReturn(null); service.findUser(ServiceTestConfiguration.BAD_USER_ID); }
@Test(expected=NoRecordsFoundException.class) public void doFindNullAccount() { when(repo.findById(999)).thenReturn(Optional.empty()); service.findUser(999); }