UserService { public Map<String, Object> login(String username, String password) { logger.debug("login in user: " + username); Map<String, Object> loginResponse = null; User account = user.findByUseridAndPasswd(username, password); if (account != null) { logger.debug("Found Account for user: " + username); account.setAuthtoken(UUID.randomUUID().toString()); account.setLogincount(account.getLogincount() + 1); account.setLastlogin(new Date()); account = user.save(account); loginResponse = new HashMap<String, Object>(); loginResponse.put("authToken", account.getAuthtoken()); loginResponse.put("accountid", account.getId()); logger.info("UserService.login success for " + username + " username::token=" + loginResponse.get("authToken")); } else { logger.warn("UserService.login failed to find username=" + username + " password=" + password); throw new AuthenticationException("Login failed for user: " + username); } return loginResponse; }  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 testLogin() { User acc = ServiceTestConfiguration.user(); when(repo.findByUseridAndPasswd(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD)).thenReturn(acc); when(repo.save(isA(User.class))).thenReturn(acc); Map<String,Object> result = service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD); assertEquals(result.get("accountid"),ServiceTestConfiguration.PROFILE_ID); assertNotNull(result.get("authToken")); }
@Test(expected=AuthenticationException.class) public void testLoginNull() { User acc = ServiceTestConfiguration.user(); when(repo.findByUseridAndPasswd(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD)).thenReturn(null); service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD); }