如何在每次测试后擦除 HSQLDB 中的数据?
我的项目中已经编写了一些 JUnit 测试,用于在设置方法中填充数据。现在我已将 Maven 添加到我的项目中,我想使用 mvn test 执行 Maven ie 中的所有测试用例。现在的问题是,每个测试类运行后我的数据库都没有被清除。我需要在每个类的测试用例运行后清除 HSQLDB。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一个简单的 SQL 脚本,在每次测试之前运行,并在开头包含以下语句:
但是我在测试之间遇到了锁定问题,添加这个脚本对我来说就像一个魅力:
I had a simple SQL script that was run before each test with the following statement at the beginning:
but I have run into lock problems between tests and adding this worked for me like a charm:
您可以通过删除架构来清除数据。默认模式称为 PUBLIC。如果执行下面的 SQL 语句,它将清除所有数据并删除所有表。
DROP SCHEMA PUBLIC CASCADE
或者,如果您需要表和架构对象定义,您可以创建一个文件:包含对象但不包含数据的数据库,并将以下属性添加到 .properties 文件中。使用该类型数据库进行测试,数据的更改没有持久化
files_read_only=true
HSQLDB 2.2.6 及更高版本中提供的最新替代方案允许您在保留表的同时清除模式中的所有数据。在下面的示例中,PUBLIC 架构被清除。
截断架构公共并提交
该语句在最新版本的 HSQLDB 中得到了增强。请参阅http://hsqldb.org/doc/2.0/guide/dataaccess- 截断语句
下的 chapt.html#dac_truncate_statement
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
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
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
按照 fredt 的建议,截断架构公共重启身份并不进行检查
为我工作。 DAO 的 JUnit 测试中的相关部分代码。
根据文档
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt .html#dac_truncate_statement
Following fredt's advice, TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK
worked for me. Relevant part of code in the JUnit test for the DAO.
According to documentation at
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_truncate_statement
我们在所有测试中所做的就是在执行最后(所有断言都通过之后)回滚事务。我们使用 Spring,默认情况下测试不会在最后提交。这可确保您始终返回到数据库的起始状态(在初始创建实体表并运行 import.sql 之后)。
即使您不使用 Spring,您也可以滚动自己的
try {} finally {}
块来回滚每个测试的已启动事务。What we do in all our tests is that we rollback the transaction at the very end of execution (after all assertions are through). We use Spring and by-default tests don't commit at the very end. This ensures that you always return to the starting state of the database (after initial creation of entity tables and running of import.sql).
Even if you don't use Spring, you can probably roll your own
try {} finally {}
block to rollback a started transaction for each test.“在测试之间清除数据库”中列出了另一个解决方案 http://www.objectpartners.com/2010/11/09/unit-testing-your-persistence-tier-code/
Another solution is listed in "Clearing the database between tests" http://www.objectpartners.com/2010/11/09/unit-testing-your-persistence-tier-code/