cbeust/testng — Issue #124: TestListenerAdapter passed through ServiceLoader is invoked twice for IConfigurationListener methods
Author: lukasj
Created: 2011-11-05 21:03:44+00:00
State: open

[using current master/6.4beta]
[note that this issue most likely applies to all listeners implementing ITestListener and IConfigurationListener interfaces]

-have following simple TestNG listener:

```java
package tnl;
import org.testng.*;

public class Tnl2 extends TestListenerAdapter {

public void onConfigurationFailure(ITestResult itr) {
super.onConfigurationFailure(itr);
System.out.println(itr);
}

public void onFinish(ITestContext testContext) {
System.out.println("Configuration method failures: " + getConfigurationFailures().size());
}
}
```
-put it into a jar with META-INF/services and add this jar to testng classpath so it can find it

-have following simple test:

```java
package test;
import org.testng.annotations.*;
public class StTest {

@BeforeClass
public void x() {
assert false;
}

@Test
public void y() {
}
}
```

-use testng with disable default listeners to use just the custom one to run the test

=> test output is:

```
[TestNG] Running:
Ant suite
[TestResult: x STATUS:FAILURE METHOD:StTest.x()[pri:0, instance:test.StTest@6b7de5ce]]
[TestResult: x STATUS:FAILURE METHOD:StTest.x()[pri:0, instance:test.StTest@6b7de5ce]]
Configuration method failures: 2
===============================================
Ant suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================
```

=> as can be seen from the ouput TestNG correctly reports that there was *1* Configuration Failure *but* listener says that there were *2* Configuration Failures

As I tried to step through the code I think I've found the suspicious code, see SuiteRunner.java, lines 539-544[1] and TestRunner.java, lines 1469-1484[2].

Since TestListenerAdapter is also an instance of IConfigurationListener SuiteRunner adds it twice into the list of interested listeners - once in the first cycle using TestRunner.addListener() and in the second using TestRunner.addConfigurationListener().

I tried to replace SuiteRunner's call to TestRunner.addListener() with TestRunner.addTestListener() and even though it fixes the problem of doubled invocation, it also breaks about 14 tests in current codebase.

Any thoughts/ideas on better place to fix this?

[1] https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/SuiteRunner.java#L539
[2] https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/TestRunner.java#L1469