Testing question
frer May 13, 2008 4:03 PMHello,
I'm quite new with Seam and although I love what I'm seeing, I have a few questions concerning the tests.
First off, I am building a test application and in it I have a AuthenticationAction that calls a AuthenticationFacade.  I call the action with this: 
(AuthenticationFacade) Component.getInstance("authenticationFacade");
Now in my test case, I want to test that the AuthenticationAction works by calling the authenticationFacade (therefore it is a integration test since it tests more than merely a class).  So far my test looks like this: 
    @Test
    public void unitTestAuthenticationOK() throws Exception {
        new ComponentTest() {
            protected void testComponents() throws Exception {
                AuthenticationAction authenticationAction = new AuthenticationAction();
                Identity identity = new Identity();
                identity.setUsername("admin");
                identity.setPassword("admin");
                setField(authenticationAction, "identity", identity);
                assert authenticationAction.authenticate();
            }
        }.run();
    }
And here is the code in the AuthenticationAction:
     @In
     private Identity identity;
     public boolean authenticate() {
          try {
               LoginUser user = Component.getInstance("authenticationFacade").login(identity.getUsername(), identity.getPassword(), null);
               if(user != null) {
                    for(LoginRole userRole: user.getLoginRoles()) {
                         identity.addRole(userRole.getHumanReadableKey());
                    }
                    for(LoginRole groupRole: user.getGroup().getRoles()) {
                         identity.addRole(groupRole.getHumanReadableKey());
                    }
               }
               return true;
          } catch (SystemException e) {
               e.printStackTrace();
               return false;
          } catch (LoginFailureException e) {
               e.printStackTrace();
               return false;
          }
     }
I get a NullPointerException when the AuthenticationAction calls the Component.getInstance() method.  I can understand it since I haven't told the test case where to look for the facade but I don't know how to do this.
More precisely my questions are: 
1- How can I tell my test case that authenticationAction will call a authenticationFacade?
2- Am I using the correct way for a service to call another service by using Component.getInstance().
Thanks for your help,
François
 
    