1 Reply Latest reply on Nov 6, 2012 8:37 AM by Scott Marshall

    TestNG - problem with @BeforeMethod and @AfterMethod running twice ?

    Scott Marshall Newbie

      I'm running TestNG 6.5.2, with Arquillian 1.0.3, running on Eclipse Helios, using the TestNG 6.5.2.20120616_1545 plugin.

      I'm running this as jboss managed, to a JBoss AS 7.1.1 server.

       

      My problem is the @BeforeMethod and @AfterMethod appear to be running twice - has anyone else seen this behaviour ?

       

      As a code snippet, I'm running :

       

      {code}

       

      package com.dizzyjock.resource.agent.cache.ws.client;

       

      import java.util.ArrayList;

      import java.util.Collection;

       

      import javax.inject.Inject;

       

      import org.jboss.arquillian.container.test.api.Deployment;

      import org.jboss.arquillian.testng.Arquillian;

      import org.jboss.shrinkwrap.api.ShrinkWrap;

      import org.jboss.shrinkwrap.api.asset.EmptyAsset;

      import org.jboss.shrinkwrap.api.asset.StringAsset;

      import org.jboss.shrinkwrap.api.spec.JavaArchive;

      import org.slf4j.Logger;

      import org.slf4j.LoggerFactory;

      import org.testng.Assert;

      import org.testng.AssertJUnit;

      import org.testng.annotations.AfterMethod;

      import org.testng.annotations.BeforeMethod;

      import org.testng.annotations.Test;

       

      import com.dizzyjock.resource.agent.action.ResMgrAgentWebService;

      import com.dizzyjock.resource.agent.cache.exceptions.CacheCommunicationException;

      import com.dizzyjock.resource.agent.cache.util.ManifestBuilder;

      import com.dizzyjock.resource.agent.vo.RecordKeyVO;

      import com.dizzyjock.resource.agent.vo.RecordValueVO;

       

      public class WebServicePoolTest extends Arquillian {

       

          private static final Logger log = LoggerFactory.getLogger(WebServicePoolTest.class);

       

          @Inject

          private WebServicePool webServicePool;

       

          @Deployment

          public static JavaArchive createDeployment() {

       

              StringAsset manifestFile = ManifestBuilder.readManifestFile(PATH_TO_MANIFEST);

       

              final JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "WebServicePoolTest.jar")

                      .addClass(WebServiceFactory.class)

                      .addClass(WebServicePool.class)

                      .addClass(DummyWebServiceFactoryImpl.class)

                      .addClass(ResMgrAgentWebService.class)

                      .addClass(RecordValueVO.class)

                      .addClass(RecordKeyVO.class)

                      .addAsResource("client.properties")

                      .addClass(CacheCommunicationException.class)

                      .setManifest(manifestFile)

                      .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

              if (log.isDebugEnabled()) {

                  log.debug(jar.toString(true));

              }

              return jar;

          }

       

          private WebServicePoolTestFixture testFixture;

       

          @BeforeMethod

          public void setUp() {

              log.info("-------- @BeforeMethod");

              testFixture = new WebServicePoolTestFixture();

          }

       

          @AfterMethod

          public void tearDown() {

              log.info("---------@@AfterMethod");

              testFixture.tearDown();

          }

       

          @Test

          public void testRetrievingASingleObject() {

              log.info("--------- testRetrievingASingleObject");

              testFixture.givenAPool();

              testFixture.whenWeGetASingleObject();

              testFixture.thenCheckReturnedObjectAndPoolParameters();

          }

       

         

       

          private class WebServicePoolTestFixture {

       

              private Collection<ResMgrAgentWebService> endPoints;

              private int expectedNumActive = 0;

              private int expectedNumIdle = 0;

              private Exception expectedException;

       

              public void tearDown() {

       

                  try {

                      for (ResMgrAgentWebService endPoint : endPoints) {

                          webServicePool.returnEndpoint(endPoint);

                      }

                  } catch (Exception e) {

                      e.printStackTrace();

                      Assert.fail("Shouldn't fail here");

                  }

       

              }

       

              public void givenAPool() {

                  endPoints = new ArrayList<ResMgrAgentWebService>();

              }

       

              public void whenWeGetASingleObject() {

                  try {

                      endPoints.add(webServicePool.borrowEndpoint());

                      expectedNumActive = 1;

                      expectedNumIdle = 0;

       

                  } catch (Exception e) {

                      Assert.fail("Shouldn't fail here");

                  }

              }

       

              public void thenCheckReturnedObjectAndPoolParameters() {

                  AssertJUnit.assertFalse("Shouldnt be empty", endPoints.isEmpty());

                  AssertJUnit.assertEquals("Should be " + expectedNumActive, expectedNumActive, endPoints.size());

                  AssertJUnit

                          .assertEquals("Should be " + expectedNumActive, expectedNumActive, webServicePool.getNumActive());

                  AssertJUnit.assertEquals("Should be " + expectedNumIdle, expectedNumIdle, webServicePool.getNumIdle());

                  if (log.isInfoEnabled()) {

                      log.info("Active is {}, ", webServicePool.getNumActive());

                      log.info("Idle is {}, ", webServicePool.getNumIdle());

                  }

              }

          }

       

      }

       

       

      {code}

       

      But my stack trace is showing :

       

      00:12:57,226  INFO com.dc123.resource.agent.cache.ws.client.WebServicePoolTest:67 - -------- @BeforeMethod

      00:12:57,501 INFO  [com.dc123.resource.agent.cache.ws.client.WebServicePoolTest] (pool-5-thread-1) -------- @BeforeMethod

      00:12:57,505 INFO  [com.dc123.resource.agent.cache.ws.client.WebServicePoolTest] (pool-5-thread-1) --------- testRetrievingASingleObject

      00:12:57,513 INFO  [com.dc123.resource.agent.cache.ws.client.WebServicePoolTest] (pool-5-thread-1) Active is 1,

      00:12:57,513 INFO  [com.dc123.resource.agent.cache.ws.client.WebServicePoolTest] (pool-5-thread-1) Idle is 0,

      00:12:57,515 INFO  [com.dc123.resource.agent.cache.ws.client.WebServicePoolTest] (pool-5-thread-1) ---------@@AfterMethod

      00:12:57,546  INFO com.dc123.resource.agent.cache.ws.client.WebServicePoolTest:73 - ---------@@AfterMethod

       

      Am I missing something ?