4 Replies Latest reply on Jan 7, 2010 6:58 AM by Nikolay Elenkov

    TestNG - How do I test Seam components?

    Charlie B Newbie

      Hi,


      I have written a simple Test to return all users from my LDAP Identity Store.
      But the tests always fail with "No application context active".


      Im not sure what im doing wrong or I have forgotten to set anything up.


      Can anyone help?




      package au.com.statewater.koncentrator.test;
      
      import java.util.List;
      
      
      import org.jboss.seam.Component;
      import org.jboss.seam.mock.SeamTest;
      import org.jboss.seam.security.management.IdentityManager;
      import org.testng.annotations.AfterClass;
      import org.testng.annotations.BeforeClass;
      import org.testng.annotations.Test;
      
      public class LdapUserTest extends SeamTest {
           
           private IdentityManager identityManager;
           
           @Test
           public void testLdapSearch() {
                
                List<String> users = identityManager.listUsers();
                
                for (String s : users) {
                     System.out.println("User: " + s);
                }
      
          }
          
          @BeforeClass
          public void init() 
          {
               identityManager = (IdentityManager) Component.getInstance("identityManager");
          }
          
          @AfterClass
          public void destroy()
          {
             
          }
      }



      Stack Trace



      java.lang.IllegalStateException: No application context active
           at org.jboss.seam.Component.forName(Component.java:1945)
           at org.jboss.seam.Component.getInstance(Component.java:2005)
           at org.jboss.seam.Component.getInstance(Component.java:1983)
           at org.jboss.seam.Component.getInstance(Component.java:1977)
           at org.jboss.seam.Component.getInstance(Component.java:1972)
           at au.com.statewater.koncentrator.test.LdapUserTest.init(LdapUserTest.java:31)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:607)
           at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:417)
           at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:154)
           at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:88)
           at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)
           at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
           at org.testng.TestRunner.runWorkers(TestRunner.java:759)
           at org.testng.TestRunner.privateRun(TestRunner.java:592)
           at org.testng.TestRunner.run(TestRunner.java:486)
           at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
           at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
           at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
           at org.testng.SuiteRunner.run(SuiteRunner.java:204)
           at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:877)
           at org.testng.TestNG.runSuitesLocally(TestNG.java:842)
           at org.testng.TestNG.run(TestNG.java:751)
           at org.testng.TestNG.privateMain(TestNG.java:914)
           at org.testng.TestNG.main(TestNG.java:887)





        • 1. Re: TestNG - How do I test Seam components?
          Michael Wohlfart Expert

          Hi Charlie,


          try this in a @BeforeClass or @BeforeMethod annotated method:


                 Lifecycle.beginCall();
                 Lifecycle.beginSession(session.getAttributes(), null);



          maybe you also need


                Identity.setSecurityEnabled(false);



          for testing
                 


          • 2. Re: TestNG - How do I test Seam components?
            Charlie B Newbie

            Hi Michael,


            Thanks for your response.


            What library is the Lifecycle class available in?


            Thanks

            • 3. Re: TestNG - How do I test Seam components?
              Charlie B Newbie

              Please ignore my previous Post.


              I've run into a couple of exceptions now with the changes you suggested.
              Im not sure what im doing wrong.


              Test Class:




              package au.com.statewater.koncentrator.test;
              
              import java.util.List;
              
              
              import org.jboss.seam.Component;
              import org.jboss.seam.contexts.Lifecycle;
              import org.jboss.seam.mock.SeamTest;
              import org.jboss.seam.security.management.IdentityManager;
              import org.testng.annotations.AfterClass;
              import org.testng.annotations.BeforeClass;
              import org.testng.annotations.Test;
              
              public class LdapUserTest extends SeamTest {
                   
                   private IdentityManager identityManager;
                   
                   @Test
                   public void testLdapSearch() {
                        
                        
                        List<String> users = identityManager.listUsers();
                        
                        for (String s : users) {
                             System.out.println("User: " + s);
                        }
                        
                  }
                  
                  @BeforeClass
                  public void init() 
                  {     
                       begin();
                       
                       Lifecycle.beginCall();
                       Lifecycle.beginSession(session.getAttributes(), null);
                       identityManager = (IdentityManager) Component.getInstance("identityManager", true);
                       
                  }
                  
                  @AfterClass
                  public void destroy()
                  {
                       end();
                       session.invalidate();
                       
                       Lifecycle.endCall();  
                       Lifecycle.endSession(session.getAttributes());
                  }
              }



              Im getting the exceptions:




              java.lang.IllegalStateException: Please end the HttpSession via org.jboss.seam.web.Session.instance().invalidate()
                   at org.jboss.seam.contexts.Lifecycle.endSession(Lifecycle.java:267)
                   at org.jboss.seam.contexts.ServletLifecycle.endSession(ServletLifecycle.java:160)
                   at org.jboss.seam.mock.AbstractSeamTest.end(AbstractSeamTest.java:903)
                   at org.jboss.seam.mock.SeamTest.end(SeamTest.java:37)




              and




              java.lang.NullPointerException
                   at au.com.statewater.koncentrator.test.LdapUserTest.testLdapSearch(LdapUserTest.java:22)
              ... Removed 22 stack frames



              when trying to invoke the component.
              It seems like the component is coming back as NULL.


              Not sure why that is.


              What am i missing?


              Thanks
              Charlie


              • 4. Re: TestNG - How do I test Seam components?
                Nikolay Elenkov Master

                Simply wrap your tests in a FacessRequest, all the mocking and lifecycle call are done for you. Read more here Integration testing Seam application user interactions