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

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

问题描述

我的项目中已经编写了一些 JUnit 测试,用于在 setup 方法中填充数据.现在我已经将 maven 添加到我的项目中,我想从 maven 中执行所有测试用例,即使用 mvn test.现在的问题是我的数据库在每个测试类运行后都没有清除.我需要在每个类的测试用例运行后清除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.

推荐答案

  1. 您可以通过删除架构来清除数据.默认架构称为 PUBLIC.如果执行下面的 SQL 语句,它将清除所有数据并删除所有表.

  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.

删除架构公共级联

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

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

files_read_only=true

HSQLDB 2.2.6 及更高版本中提供的最新替代方案允许您在保留表的同时清除架构中的所有数据.在下面的示例中,PUBLIC 架构被清除.

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.

截断架构公开并提交

该语句在最新版本的 HSQLDB 中得到了增强.见 http://hsqldb.org/doc/2.0/guide/dataaccess-截断语句

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天全站免登陆