@afterTest method called twice

141 views
Skip to first unread message

mmarkus

unread,
Dec 11, 2008, 2:39:00 PM12/11/08
to testng-users
Hi,

Running the code:

@Test(groups = "functional", testName = "sadsadsa")
public class Base
{
private int value = -5;

static int count;

public Base(int value)
{
this.value = value;
}

public Base()
{
}

@Factory
public static Object[] createInstances()
{
Base base = new Base(1);
Base base2 = new Base(2);
return new Object[]{base, base2};
}


@BeforeClass
public void beforeTest()
{
System.out.println("Base.beforeTest" + value);
}

@AfterClass
public void afterTest()
{
System.out.println("Base.afterTest" + value);
}

public void testBaseAaaaaa()
{
System.out.println(getThreadName() + "aaaaaaaaaaaa" + value);
}

@BeforeMethod
public void beforeMethod()
{
System.out.println("Base.beforeMethod" + value);
}

@AfterMethod
public void afterMethod()
{
System.out.println("Base.afterMethod" + value);
}

public void testBaseBbbbbbb()
{
count++;
// if (count == 2) throw new RuntimeException();
System.out.println(getThreadName() + "bbbbbbbbbbbbb" + value);
}

protected String getThreadName()
{
return "[" + getClass() + " ************ -> " +
Thread.currentThread().getName() + "] ";
}
}

produces the output:
Base.beforeTest1
Base.beforeMethod1
[pool-1-thread-1] Starting test testBaseBbbbbbb
(org.jboss.cache.testng.Base)
[class org.jboss.cache.testng.Base ************ -> pool-1-thread-1]
bbbbbbbbbbbbb1
Base.afterMethod1
[pool-1-thread-1] Test testBaseBbbbbbb(org.jboss.cache.testng.Base)
succeded.
Testsuite execution progress: tests succeded 1, failed 0, skipped 0.
Base.beforeMethod1
[pool-1-thread-1] Starting test testBaseAaaaaa
(org.jboss.cache.testng.Base)
[class org.jboss.cache.testng.Base ************ -> pool-1-thread-1]
aaaaaaaaaaaa1
Base.afterMethod1
[pool-1-thread-1] Test testBaseAaaaaa(org.jboss.cache.testng.Base)
succeded.
Testsuite execution progress: tests succeded 2, failed 0, skipped 0.
Base.afterTest1
Base.beforeTest2
Base.beforeMethod2
[pool-1-thread-1] Starting test testBaseBbbbbbb
(org.jboss.cache.testng.Base)
[class org.jboss.cache.testng.Base ************ -> pool-1-thread-1]
bbbbbbbbbbbbb2
Base.afterMethod2
[pool-1-thread-1] Test testBaseBbbbbbb(org.jboss.cache.testng.Base)
succeded.
Testsuite execution progress: tests succeded 3, failed 0, skipped 0.
Base.afterTest2
Base.beforeMethod2
[pool-1-thread-1] Starting test testBaseAaaaaa
(org.jboss.cache.testng.Base)
[class org.jboss.cache.testng.Base ************ -> pool-1-thread-1]
aaaaaaaaaaaa2
Base.afterMethod2
[pool-1-thread-1] Test testBaseAaaaaa(org.jboss.cache.testng.Base)
succeded.
Testsuite execution progress: tests succeded 4, failed 0, skipped 0.
Base.afterTest2
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.454
sec

As you can see Base.afterTest2 gets called *twice*, even before
running a test method in the given test.
I would expect only one invocation of that method - or am I doing
something wrong?

I run the suite from maven:
<configuration>
<parallel>tests</parallel>
<threadCount>10</threadCount>
<forkMode>none</forkMode>
<trimStackTrace>false</trimStackTrace>
<properties>
<property>
<name>listener</name>

<value>org.jboss.cache.util.UnitTestTestNGListener</value>
</property>
</properties>
</configuration>
I need this as I am refactoring our test suite, any help is much
appreciated.



Cédric Beust ♔

unread,
Dec 11, 2008, 2:56:52 PM12/11/08
to testng...@googlegroups.com
It's a bit hard to read your example, but it seems to me you have two test methods so I'm not surprised to see two invocations of @AfterMethod.

If this doesn't solve your problem, can you post a simpler example?

Thanks.

--
Cedric

--
Cédric


mmarkus

unread,
Dec 12, 2008, 3:06:20 AM12/12/08
to testng-users
Sorry for being so verbose, I'll try to explain in words.
So, the scenario is:
I have a @Factory that creates two instances of the same test.
The test has an @BeforeClass, an @AfterClass and an @BeforeMethod.

At runtine, I would expect the following sequence of methods to be
executed twice:
beforeClass
beforeMethod
beforeMethod
....beforeMethod....
afterClass
beforeClass
beforeMethod
beforeMethod
....beforeMethod....
afterClass

Now my issue is that the sequence is as follows:
beforeClass
beforeMethod
beforeMethod
....beforeMethod....
afterClass
beforeClass
beforeMethod
beforeMethod
afterClass //
<---------------------------------------------------------- so
afterClass gets executed twice for the same test instance (3 times
overall)
....beforeMethod....
afterClass

hope I've been more concise
(You can use the attached code to reproduce the issue.)
Cheers,
Mircea
> ***Cédric
> *
Reply all
Reply to author
Forward
0 new messages