2 Replies Latest reply on May 14, 2008 3:27 PM by frer

    Testing question

    frer

      Hello,


      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

        • 1. Re: Testing question
          pmuir

          You can't instantiate a Seam component using the new operator.

          • 2. Re: Testing question
            frer

            Hi Pete,


            Thank you for your answer. 


            Even if I use it with the Component.getInstance(authenticationAction), I still haven't got the reference to my authenticationFacade.


            How can I set the component in the seam context so that when the action calls the facade, it knows where to find it.


            I may not make much sense so please bare with me.  I don't yet understand everything behind seam.


            Thank you,


            François