如何在每次测试后从我的HSQLDB中擦除数据? [英] How can I wipe data from my HSQLDB after every test?

查看:88
本文介绍了如何在每次测试后从我的HSQLDB中擦除数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中已经写了一些JUnit测试,这些测试用于在安装方法中填充数据。现在我已经将Maven添加到了我的项目中,并且想要执行所有测试用例,例如使用mvn测试。现在的问题是我的数据库在每个测试类运行后都没有被清除。我需要在每个类的测试用例都运行后清除HSQLDB。 您可以通过删除模式来清除数据。默认模式称为PUBLIC。如果您执行下面的SQL语句,它将清除所有数据并删除所有表。



DROP SCHEMA PUBLIC CASCADE


<或者,如果需要表和模式对象定义,则可以创建一个文件:包含对象但不包含数据的数据库,并将以下属性添加到.properties文件中。使用这种类型的数据库进行测试,数据的更改不会持久

files_read_only = true

  • HSQLDB 2.2.6及更高版本中提供的最新备选方案允许您在保留表格的同时清除模式中的所有数据。在下面的示例中,清除了PUBLIC模式。



    TRUNCATE SCHEMA公共和COMMIT



    在最新版本的HSQLDB中得到了增强。请参阅 http://hsqldb.org/doc/2.0/guide/dataaccess-在截断语句下的chapt.html#dac_truncate_statement


    I had some JUnit tests already written in my project which used to populate data in the setup method. Now I have added maven to my project and I want to execute all test cases form maven i.e. using mvn test. The problem now is that my data base is not cleared after every test class has run. I need to clear the HSQLDB after test cases of each class have run.

    解决方案

    1. You can clear the data by dropping the schema. The default schema is called PUBLIC. If you execute the SQL satement below, it will clear all data and drop all tables.

      DROP SCHEMA PUBLIC CASCADE

    2. Alternatively, if you need the table and schema object definitions, you can create a file: database containing the objects but no data, and add the property below to the .properties file. Using this type of database for tests, the changes to data are not persisted

      files_read_only=true

    3. The latest alternative, available in HSQLDB 2.2.6 and later allows you to clear all the data in a schema while keeping the tables. In the example below, the PUBLIC schema is cleared.

      TRUNCATE SCHEMA public AND COMMIT

      This statement has been enhanced in the latest versions of HSQLDB. See http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_truncate_statement under Truncate Statement

    这篇关于如何在每次测试后从我的HSQLDB中擦除数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆