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