4 Replies Latest reply on Dec 12, 2008 12:29 PM by Matt Nirgue

    Using BeforeTest and AfterTest in ComponentTest

    Francois Eric Novice

      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