我正在编写一些测试类并从基础测试类扩展。但问题是即使我锁定了 isInited 变量,它为每个类运行一次。它应该运行一次并对其进行初始化,之后不应再次调用它,但它调用了 3 次,因为我有 3 个从基类扩展的类。请看下文。
Java 1.8 和 TestNG
public class BaseTest(){
private static isInited;
@BeforeClass
public void init(){
synchronized (BaseTest.class) {
//here even though I lock and initialize the variable this code is still called once for each class. I do not understand why this happens?
if (!isInited) {
//do some init
isInited=true;
}
}
}
public class TestClass1 extends BaseTest{
@BeforeClass
public void setup(){
//setup somethings
}
//test methods
}
public class TestClass2 extends BaseTest{
@BeforeClass
public void setup(){
//setup somethings
}
//test methods
}
public class TestClass3 extends BaseTest{
@BeforeClass
public void setup(){
//setup somethings
}
//test methods
}