1 Reply Latest reply on Mar 15, 2010 1:03 PM by Grzegorz Bernas

    TestNG and user Principal

    Grzegorz Bernas Newbie

      Hi!
      I try to write test that uses Session Bean with



      @In
       Principal userPrincipal;





      public class MyTest extends SeamTest {
          private Log log = Logging.getLog(KwerendyTest.class);
      
          @SuppressWarnings("unused")
          @Test
          private void testMyBean() throws Exception {
              new ComponentTest() {
      
                  @Override
                  protected void testComponents() throws Exception {
                      EntityManager entityManager = (EntityManager) getInstance("entityManager");
      
                      setValue("#{identity.username}", "admin");
                      setValue("#{identity.password}", "");
      
                      invokeMethod("#{identity.login}");
                      
                      User user = (User) getInstance("logedInUser");     
      
                      /* ... */
                  }
              }.run();
          }
      }
      



      @Name("logedInUser")
      @Scope(ScopeType.SESSION)
      @AutoCreate
      public class LogedInUser {
      
          @Logger
          Log log;
          @In
          private Session hibernateSession;
      
          @In
          Principal userPrincipal;
      
          @Unwrap
          public User getUser() {
      
              User user = null;
              String username = userPrincipal.getName();
      
              /* ... */
          }
      }
      



      after that I get this error:


      org.jboss.seam.RequiredException: @In attribute requires non-null value: logedInUser.userPrincipal
      



      Why Principal is null?

        • 1. Re: TestNG and user Principal
          Grzegorz Bernas Newbie

          I resolve my problem.
          First of all, I used wrong context. This should be an new FacesRequest();
          To set a principal, I need to override method getPrincipalName, without need of login.


           new FacesRequest() {
                      public String getPrincipalName() {
                          return "admin";
                      };
                      
                      @Override
                      protected void updateModelValues() throws Exception {
                          setValue("#{identity.username}", "admin");
                          setValue("#{identity.password}", "");
                      }
          
                      @Override
                      protected void invokeApplication() {
                          log.info(invokeMethod("#{identity.login}"));
                          log.info("principal: " + getValue("#{userPrincipal.name}"));                
                          
                          /* ... */
                      }
          
                  }.run();
          



          and it works,


           
          @In
          Principal userPrincipal;
          



          is injected.