Created
September 17, 2012 08:22
-
-
Save krmahadevan/3736157 to your computer and use it in GitHub Desktop.
How to use ITestNGListenerFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testng.samples; | |
import org.testng.IExecutionListener; | |
import org.testng.ITestNGListener; | |
import org.testng.ITestNGListenerFactory; | |
import org.testng.annotations.Listeners; | |
import org.testng.annotations.Test; | |
/* | |
The following points are worth keeping in mind : | |
1. The class that implements ITestNGListenerFactory must be made known to TestNG only via the @Listeners annotation. | |
Using <listeners> tag in the suite file will NOT cause this listener to be invoked, | |
because this is a "Test" class listener. | |
2. As per the javadocs of ITestNGListenerFactory, ONLY one instance of the object that implements ITestNGListenerFactory | |
must exist in memory. | |
*/ | |
@Listeners(PlayingWithListenerFactory.class) | |
public class PlayingWithListenerFactory implements ITestNGListenerFactory, ITestNGListener { | |
@Test | |
public void foo() { | |
System.out.println("Hello world foo()"); | |
} | |
@Override | |
public ITestNGListener createListener(Class<? extends ITestNGListener> listenerClass) { | |
System.out.println("create listener called"); | |
return new MyExecutionListener(); | |
} | |
public static class MyExecutionListener implements IExecutionListener { | |
public MyExecutionListener() { | |
System.out.println("Listener instantiated"); | |
} | |
@Override | |
public void onExecutionStart() { | |
System.out.println("exec start"); | |
} | |
@Override | |
public void onExecutionFinish() { | |
System.out.println("exec finished"); | |
} | |
} | |
} |
The following points are worth keeping in mind :
- The class that implements ITestNGListenerFactory must be made known to TestNG only via the @listeners annotation. Using tag in the suite file will NOT cause this listener to be invoked, because this is a "Test" class listener.
- As per the javadocs of ITestNGListenerFactory, ONLY one instance of the object that implements ITestNGListenerFactory must exist in memory.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following points are worth keeping in mind :