Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java项目中运行所有junit之前加载一次HSQLDB_Java_Hibernate_Maven_Junit - Fatal编程技术网

如何在Java项目中运行所有junit之前加载一次HSQLDB

如何在Java项目中运行所有junit之前加载一次HSQLDB,java,hibernate,maven,junit,Java,Hibernate,Maven,Junit,我的项目不是基于Spring的,它是一个带有Hibernate.Building tool-Maven的java 在运行junits之前,我正在将数据从一个数据库加载到HSQL DB 我的DB util类: public class DatabaseUtil { SchemaLoad schemaLoad = new SchemaLoad(); DataLoad dataLoad = new DataLoad(); boolean dataLoaded = false;

我的项目不是基于Spring的,它是一个带有Hibernate.Building tool-Maven的java

在运行junits之前,我正在将数据从一个数据库加载到HSQL DB

我的DB util类:

public class DatabaseUtil {
    SchemaLoad schemaLoad = new SchemaLoad();
    DataLoad dataLoad = new DataLoad();
    boolean dataLoaded = false;

    static final String filename1 = "test1.txt";
    static final String filename2 = "text2.txt";

    void dbLoad() throws SQLException {
        if (!dataLoaded) {
            schemaLoad.cloneSchema(filename1);
            dataLoad.exportData(filename2);
            System.out.println("***********executed**********8");
            dataLoaded = true;
        }
    }
}
第一个测试用例:

public class TestCase {

    TrainRepository trainRepository = new TrainRepositoryImpl();
    DatabaseUtil databaseUtil = new DatabaseUtil();

    @BeforeClass
    private void setUp() throws SQLException {
        databaseUtil.dbLoad();

    }

    @Test
    private void positiveTestCaseForTrainRepo() throws Exception {

        //TestCases
    }
第二个测试用例:

公共类测试用例1{

AirRepository airRepository = new AirRepositoryImpl();
DatabaseUtil databaseUtil = new DatabaseUtil();

@BeforeClass
private void setUp() throws SQLException {
    databaseUtil.dbLoad();

}

@Test
private void positiveTestCaseForAirRepo() throws Exception {

    //TestCases
}
这两个测试用例都运行良好,但它在每个junit上都执行databaseUtil.dbLoad();方法

我的问题是,在第一个junit开始之前,我只需要加载一次数据库,并且需要设置一些指示符。进一步的junit需要检查DB实例,如果DB实例在那里,它不应该加载数据,ie DatabaseUtil类需要是singleton

在mvn安装阶段,所有junits都通过maven Supplere插件运行

请帮助我实现这一点。

每次都将调用void dbLoad()

然后使用一个静态变量来跟踪

static boolean dataLoaded = false;

如果您不使用spring,您需要自己实现缓存。您有几个选项。使用静态字段进行某种同步(如果您使用/计划使用线程)。另一个选项是切换到testng,它为您提供了
@BeforeGroup
功能,这样您就可以标记所有的db测试,并在之前运行初始化。

您是说对属于同一测试套件的每个测试调用dbLoad()(在本例中是TestCase1)?如果是这样的话,那就不应该发生,因为BeforeClass是为一个测试套件运行一次而设计的。你所描述的听起来像是一个BeforeClass注释,而不是BeforeClass。如果我将其标记为dataload为static,我会在setUp methodjava.lang上得到初始化异常。异常:Method setUp()应该是静态的我不明白:dataLoaded应该是DatabaseUtil的静态变量,并且只能在dbload中访问。