1 package test;
2 
3 import org.testng.annotations.AfterTest;
4 import org.testng.annotations.Test;
5 
6 /**
7  * this test verifys that the test class is instantiated exactly once
8  * regardless of how many test methods we have, showing that TestNG
9  * semantics is quite different from JUnit
10  */
11 public class CtorCalledOnce {
12   public static int instantiated = 0;
CtorCalledOnce()13   public CtorCalledOnce() {
14     instantiated++;
15   }
16 
17   @Test
testMethod1()18   public void testMethod1(){
19     assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
20   }
21 
22   @Test
testMethod2()23   public void testMethod2(){
24     assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
25   }
26 
27   @Test
testMethod3()28   public void testMethod3(){
29     assert instantiated == 1 : "Expected 1, was invoked " + instantiated + " times";
30   }
31 
32   @AfterTest
afterTest()33   public void afterTest() {
34     instantiated = 0;
35   }
36 
37 }