Guest User

Untitled

a guest
Dec 16th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package truckz.web;
  6.  
  7. import org.junit.Assert;
  8. import org.junit.Test;
  9.  
  10. /**
  11.  *
  12.  * @author Eddy
  13.  */
  14. public class LoginBeanTest {
  15.  
  16.     class Verification { public boolean verified; }
  17.  
  18.     @Test
  19.     public void authenticateReturnsCorrectOutcome() {
  20.         final String username = "joebloggs";
  21.         final String password = "secret";
  22.  
  23.         LoginBeanOut stubLoginBeanOut = new LoginBeanOut() {
  24.             @Override
  25.             public LoginResult login(String username, String password) {
  26.                 return LoginResult.SUCCESS;
  27.             }
  28.         };
  29.  
  30.         FacesContextWrapper stubFacesContextWrapper = new FacesContextWrapper() {
  31.             @Override
  32.             public void addMessage(String componentId, String message) { }
  33.         };
  34.  
  35.         LoginBean loginBean = new LoginBean(stubLoginBeanOut, stubFacesContextWrapper);
  36.         loginBean.setUsername(username);
  37.         loginBean.setPassword(password);
  38.  
  39.         String outcome = loginBean.login();
  40.  
  41.         Assert.assertNotNull(outcome);
  42.     }
  43.  
  44.     @Test
  45.     public void authenticateSetsMessageForComponent() {
  46.         final String username = "joebloggs";
  47.         final String password = "secret";
  48.  
  49.         final Verification verification = new Verification();
  50.  
  51.         LoginBeanOut stubLoginBeanOut = new LoginBeanOut() {
  52.             @Override
  53.             public LoginResult login(String username, String password) {
  54.                 return LoginResult.UNKNOWN_USERNAME;
  55.             }
  56.         };
  57.  
  58.         FacesContextWrapper stubFacesContextWrapper = new FacesContextWrapper() {
  59.             @Override
  60.             public void addMessage(String componentId, String message) {
  61.                 verification.verified = (componentId != null) && (message != null);
  62.             }
  63.         };
  64.  
  65.         LoginBean loginBean = new LoginBean(stubLoginBeanOut, stubFacesContextWrapper);
  66.         loginBean.setUsername(username);
  67.         loginBean.setPassword(password);
  68.  
  69.         String unused = loginBean.login();
  70.  
  71.         Assert.assertTrue(verification.verified);
  72.     }
  73. }
Add Comment
Please, Sign In to add comment