4 Replies Latest reply on Dec 12, 2008 12:29 PM by matt.nirgue

    Using BeforeTest and AfterTest in ComponentTest

    frer

      Hi,


      I'm pretty new to testng and seam but am trying to get a test working in which I would have a setup and a teardown for each test.


      If I try to do it without separation it works:


              @Test
              public void testChangeName() throws Exception {
                      new ComponentTest() {
                              protected void testComponents() throws Exception {
                                      persistenceSession = (Session) Component.getInstance("persistenceSession");
                                      groupFacade = (LoginGroupFacade<LoginGroup>) Component.getInstance(LoginGroupFacadeImpl.class);
      
                                      persistenceSession.getTransaction().begin();
      
                                      group1 = new LoginGroup();
                                      group1.setName("testGroup1");
      
                                      group2 = new LoginGroup();
                                      group2.setName("testGroup2");
                                      
                                      group1 = groupFacade.save(group1);
                                      group2 = groupFacade.save(group2);
                              
                                      persistenceSession.getTransaction().commit();
      
                                      
                                      
                                      
                                      persistenceSession.getTransaction().begin();
      
                                      group1.setName("testGroup1_mod");
                                      groupFacade.save(group1);
                                      group2.setName("testGroup2_mod");
                                      groupFacade.save(group2);
                                      
                                      persistenceSession.getTransaction().commit();
      
                              
                              
                              
                                      persistenceSession.getTransaction().begin();
                                      group1 = groupFacade.fetchById(group1.getId());
                                      group2 = groupFacade.fetchById(group2.getId());
      
                                      groupFacade.delete(group1);
                                      groupFacade.delete(group2);
                                      
                                      persistenceSession.getTransaction().commit();
                              
                              }
                      }.run();
              }
      



      As you can see there are 3 different sections to my test and this is what I would like to do:


           @BeforeTest
           public void beforeTest() throws Exception {
                //Create test data
                new ComponentTest() {
                     protected void testComponents() throws Exception {
                          persistenceSession = (Session) Component.getInstance("persistenceSession");
                          groupFacade = (LoginGroupFacade<LoginGroup>) Component.getInstance(LoginGroupFacadeImpl.class);
      
                          persistenceSession.getTransaction().begin();
      
                          group1 = new LoginGroup();
                          group1.setName("testGroup1");
      
                          group2 = new LoginGroup();
                          group2.setName("testGroup2");
                          
                          group1 = groupFacade.save(group1);
                          group2 = groupFacade.save(group2);
                     
                          persistenceSession.getTransaction().commit();
                     }
                }.run();
           }
           
           @AfterTest
           public void afterTest() throws Exception {
                //Delete test data
                new ComponentTest() {
                     protected void testComponents() throws Exception {
                          try {
                               persistenceSession.getTransaction().begin();
                               group1 = groupFacade.fetchById(group1.getId());
                               group2 = groupFacade.fetchById(group2.getId());
      
                               groupFacade.delete(group1);
                               groupFacade.delete(group2);
                               
                               persistenceSession.getTransaction().commit();
                          } catch (Exception e) {
                               System.err.println("");
                          }
                     }
                }.run();
           }
      
           @Test
           public void testChangeName() throws Exception {
                new ComponentTest() {
                     protected void testComponents() throws Exception {
                          persistenceSession.getTransaction().begin();
      
                          group1.setName("testGroup1_mod");
                          groupFacade.save(group1);
                          group2.setName("testGroup2_mod");
                          groupFacade.save(group2);
                          
                          persistenceSession.getTransaction().commit();
                     }
                }.run();
           }
      
      



      Unfortunately, I get the following exception when I launch this test:


      FAILED CONFIGURATION: @BeforeTest beforeTest
      java.lang.NullPointerException
           at org.jboss.seam.servlet.ServletSessionMap.get(ServletSessionMap.java:54)
           at org.jboss.seam.contexts.BasicContext.get(BasicContext.java:48)
           at org.jboss.seam.contexts.Contexts.lookupInStatefulContexts(Contexts.java:199)
           at org.jboss.seam.Component.getInstance(Component.java:1842)
           at org.jboss.seam.Component.getInstance(Component.java:1837)
           at org.mdarad.global.tests.FirstTestCase$1.testComponents(FirstTestCase.java:74)
           at org.jboss.seam.mock.BaseSeamTest$ComponentTest.run(BaseSeamTest.java:169)
           at org.mdarad.global.tests.FirstTestCase.beforeTest(FirstTestCase.java:90)
      ... Removed 19 stack frames
      SKIPPED CONFIGURATION: @AfterTest afterTest
      



      I'm wondering how I can correct this because it is important for me to have a setup and teardown method since many of my tests have common logic.


      Thank you,


      François

        • 1. Re: Using BeforeTest and AfterTest in ComponentTest
          chawax

          I have the same problem. Did you find a solution ?

          • 2. Re: Using BeforeTest and AfterTest in ComponentTest
            chawax

            I think I found the problem : it looks like ComponentTest class works only inside method with @Test annotation, but not with @BeforeTest, @BeforeSuite, and others. I don't know if it's something intented, but it looks very strange to me (note I had the problem with latest 2.1.0.A1 relase).


            To workaround this, I used TestNG dependent groups.


            In your case, we could have :


            @Test(groups="startup")
                 public void beforeTest() throws Exception {
            



            then


            @Test(groups="tests",dependsOnGroups="startup")
                 public void testChangeName() throws Exception {



            and finally


            @Test(dependsOnGroups="tests")
                 public void afterTest() throws Exception {
            


            More work than using @BeforeTest and @AfterTest annotations, but I can't see other way to workaround this problem for the moment. Hope it will help.

            • 3. Re: Using BeforeTest and AfterTest in ComponentTest
              frer

              That's great...I haven't thought of that.  I'll give it a shot (sorry for the delay...I was unfortunately off my seam project lately).


              • 4. Re: Using BeforeTest and AfterTest in ComponentTest
                matt.nirgue

                You can't use a ComponentTest, FacesRequest or NonFacesRequest in a @<Before/After>Class method because Seam is ONLY initialized when starting a @Test method...


                Therefore, you can't access to any context, component or anything related to Seam outside of a @Test method... This is just how it is...


                The solution Olivier gave you should work just fine though. You can also use dependsOnMethods if you don't want nor need to define groups.