UserService { public boolean isValidLocalLogin(String userName, String password) { return allSharedPreferences.fetchRegisteredANM().equals(userName) && repository.canUseThisPassword(password); } UserService(Repository repository, AllSettings allSettings, AllSharedPreferences allSharedPreferences, HTTPAgent httpAgent, Session session,
                       DristhiConfiguration configuration, SaveANMLocationTask saveANMLocationTask); boolean isValidLocalLogin(String userName, String password); LoginResponse isValidRemoteLogin(String userName, String password); void localLogin(String userName, String password); void remoteLogin(String userName, String password, String anmLocation); boolean hasARegisteredUser(); void logout(); void logoutSession(); boolean hasSessionExpired(); String switchLanguagePreference();  }
@Test public void shouldConsiderALocalLoginValidWhenUsernameMatchesRegisteredUserAndPasswordMatchesTheOneInDB() { when(allSharedPreferences.fetchRegisteredANM()).thenReturn("ANM X"); when(repository.canUseThisPassword("password Z")).thenReturn(true); assertTrue(userService.isValidLocalLogin("ANM X", "password Z")); verify(allSharedPreferences).fetchRegisteredANM(); verify(repository).canUseThisPassword("password Z"); }
@Test public void shouldConsiderALocalLoginInvalidWhenRegisteredUserDoesNotMatch() { when(allSharedPreferences.fetchRegisteredANM()).thenReturn("ANM X"); assertFalse(userService.isValidLocalLogin("SOME OTHER ANM", "password")); verify(allSharedPreferences).fetchRegisteredANM(); verifyZeroInteractions(repository); }
@Test public void shouldConsiderALocalLoginInvalidWhenRegisteredUserMatchesButNotThePassword() { when(allSharedPreferences.fetchRegisteredANM()).thenReturn("ANM X"); when(repository.canUseThisPassword("password Z")).thenReturn(false); assertFalse(userService.isValidLocalLogin("ANM X", "password Z")); verify(allSharedPreferences).fetchRegisteredANM(); verify(repository).canUseThisPassword("password Z"); }