cbeust/testng — Issue #184: Problems with @(Before|After)Suite and @(Before|After)Test
Author: ash2k
Created: 2012-03-03 08:01:22+00:00
State: open

Code, demonstrating issues:

```
public abstract class AbstractTest {
protected final Logger log = LoggerFactory.getLogger(getClass());

@BeforeSuite
public final void beforeAbstractSuite() {
log.debug("beforeAbstractSuite()");
}

@BeforeTest
public final void beforeAbstractTest() {
log.debug("beforeAbstractTest()");
}

@BeforeClass
public final void beforeAbstractClass() {
log.debug("beforeAbstractClass()");
}

@BeforeMethod
public final void beforeAbstractMethod() {
log.debug("beforeAbstractMethod()");
}

@Test
public final void abstractTest() {
log.debug("abstractTest()");
}

@AfterMethod
public final void afterAbstractMethod() {
log.debug("afterAbstractMethod()");
}

@AfterClass
public final void afterAbstractClass() {
log.debug("afterAbstractClass()");
}

@AfterTest
public final void afterAbstractTest() {
log.debug("afterAbstractTest()");
}

@AfterSuite
public final void afterAbstractSuite() {
log.debug("afterAbstractSuite()");
}
}
```

```
public class RealTest1 extends AbstractTest {
@Test
public void test1() {
log.debug("test1");
}
}
```

```
public class RealTest2 extends AbstractTest {
@Test
public void test2() {
log.debug("test2");
}
}
```

testng.xml:

```

```

After running tests the logger output looks like this:

```
13:43:05 [main] DEBUG RealTest2:20 - beforeAbstractSuite()
13:43:05 [TestNG] DEBUG RealTest2:25 - beforeAbstractTest()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:30 - beforeAbstractClass()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:30 - beforeAbstractClass()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:40 - abstractTest()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:40 - abstractTest()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:8 - test2
13:43:05 [pool-3-thread-1] DEBUG RealTest1:8 - test1
13:43:05 [pool-3-thread-2] DEBUG RealTest2:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:50 - afterAbstractClass()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:50 - afterAbstractClass()
13:43:05 [TestNG] DEBUG RealTest2:55 - afterAbstractTest()
13:43:05 [main] DEBUG RealTest2:60 - afterAbstractSuite()
```

I see two problems here:
- Methods, annotated with `@(Before|After)Suite` and `@(Before|After)Test` are not executed before/after each child class. I mean i expected each child class to contribute invocations of the corresponding methods.
- If the above expectations are correct then there is another issue: those methods are executed with wrong threads. This can be the cause of visibility/concurrency problems.