1 package test.listeners;
2 
3 import java.util.concurrent.atomic.AtomicInteger;
4 
5 import org.testng.Assert;
6 import org.testng.IConfigurationListener;
7 import org.testng.ISuite;
8 import org.testng.ISuiteListener;
9 import org.testng.ITestResult;
10 import org.testng.annotations.Listeners;
11 import org.testng.annotations.Test;
12 
13 import test.listeners.SuiteAndConfigurationListenerTest.MyListener;
14 
15 /**
16  * Check that if a listener implements IConfigurationListener additionally to
17  * ISuiteListener, ISuiteListener gets invoked exactly once.
18  *
19  * @author Mihails Volkovs
20  */
21 @Listeners(MyListener.class)
22 public class SuiteAndConfigurationListenerTest {
23   public static class MyListener implements ISuiteListener, IConfigurationListener {
24 
25     private static volatile AtomicInteger started = new AtomicInteger(0);
26 
MyListener()27     public MyListener() {
28     }
29 
30     @Override
onStart(ISuite suite)31     public void onStart(ISuite suite) {
32       started.incrementAndGet();
33     }
34 
35     @Override
onFinish(ISuite suite)36     public void onFinish(ISuite suite) {
37     }
38 
39     @Override
onConfigurationSuccess(ITestResult itr)40     public void onConfigurationSuccess(ITestResult itr) {
41     }
42 
43     @Override
onConfigurationFailure(ITestResult itr)44     public void onConfigurationFailure(ITestResult itr) {
45     }
46 
47     @Override
onConfigurationSkip(ITestResult itr)48     public void onConfigurationSkip(ITestResult itr) {
49     }
50 
51   }
52 
53   @Test
bothListenersShouldRun()54   public void bothListenersShouldRun() {
55     Assert.assertEquals(MyListener.started.get(), 1, "ISuiteListener was not invoked exactly once:");
56   }
57 
58 }
59