### Question:
AdministratorLoginBean implements Serializable { public String login() { return isSuccess ? INDEX_PAGE : LOGIN_PAGE; }  String login(); void login(final ActionEvent ae); void setAdminAuthManager(final AuthenticationManager adminAuthManager); String getUsername(); void setUsername(final String username); String getPassword(); void setPassword(final String password); static final String EMPTY_FIELD_MESSAGE; static final String INVALID_LOGIN_MESSAGE; }### Answer:
@Test public void loginEmptyUsername() throws Exception { setCredentials(empty, password); loginBean.login(null); assertAccessDenied(); assertEmptyField(); }
@Test public void loginEmptyPassword() throws Exception { setCredentials(username, empty); loginBean.login(null); assertAccessDenied(); assertEmptyField(); }
@Test public void loginUsernameInvalid() throws Exception { final Authentication request = new UsernamePasswordAuthenticationToken( username, password); Mockito.when(manager.authenticate(request)).thenThrow( new UsernameNotFoundException("")); setCredentials(username, password); loginBean.login(null); assertAccessDenied(); verifyMessage(AdministratorLoginBean.INVALID_LOGIN); verifyLoginManager(); }
@Test public void loginPasswordInvalid() throws Exception { final Authentication request = new UsernamePasswordAuthenticationToken( username, password); Mockito.when(manager.authenticate(request)).thenThrow( new BadCredentialsException("")); setCredentials(username, password); loginBean.login(null); verifyMessage(AdministratorLoginBean.INVALID_LOGIN); verifyLoginManager(); }
@Test public void loginSuccessfull() throws Exception { Mockito.when(manager.authenticate(request)).thenReturn(request); final SecurityContext securityContext = Mockito .mock(SecurityContext.class); SecurityContextHolder.setContext(securityContext); setCredentials(username, password); loginBean.login(null); assertEquals(loginBean.login(), AdministratorLoginBean.INDEX_PAGE); Mockito.verify(securityContext).setAuthentication(request); verifyLoginManager(); }