如何在每次测试后擦除 HSQLDB 中的数据?

发布于 2024-10-17 07:03:53 字数 150 浏览 2 评论 0原文

我的项目中已经编写了一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

め七分饶幸 2024-10-24 07:03:54

我有一个简单的 SQL 脚本,在每次测试之前运行,并在开头包含以下语句:

TRUNCATE SCHEMA public AND COMMIT;

但是我在测试之间遇到了锁定问题,添加这个脚本对我来说就像一个魅力:

@After
public void after() throws Exception {
    if (entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().rollback();
    }
}

I had a simple SQL script that was run before each test with the following statement at the beginning:

TRUNCATE SCHEMA public AND COMMIT;

but I have run into lock problems between tests and adding this worked for me like a charm:

@After
public void after() throws Exception {
    if (entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().rollback();
    }
}
飘然心甜 2024-10-24 07:03:53
  1. 您可以通过删除架构来清除数据。默认模式称为 PUBLIC。如果执行下面的 SQL 语句,它将清除所有数据并删除所有表。

    DROP SCHEMA PUBLIC CASCADE

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

    files_read_only=true

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

    截断架构公共并提交

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

    下的 chapt.html#dac_truncate_statement

  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

苍暮颜 2024-10-24 07:03:53

按照 fredt 的建议,截断架构公共重启身份并不进行检查
为我工作。 DAO 的 JUnit 测试中的相关部分代码。

@After
public void tearDown() {
    try {
        clearDatabase();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}


public void clearDatabase() throws Exception {
  DataSource ds = (DataSource) SpringApplicationContext.getBean("mydataSource");
  Connection connection = null;
  try {
    connection = ds.getConnection();
    try {
      Statement stmt = connection.createStatement();
      try {
        stmt.execute("TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK");
        connection.commit();
      } finally {
        stmt.close();
      }
    } catch (SQLException e) {
        connection.rollback();
        throw new Exception(e);
    }
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

根据文档
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt .html#dac_truncate_statement

如果指定了 RESTART IDENTITY,则所有表 IDENTITY 序列和所有
架构中的 SEQUENCE 对象将重置为其起始值

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.

@After
public void tearDown() {
    try {
        clearDatabase();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}


public void clearDatabase() throws Exception {
  DataSource ds = (DataSource) SpringApplicationContext.getBean("mydataSource");
  Connection connection = null;
  try {
    connection = ds.getConnection();
    try {
      Statement stmt = connection.createStatement();
      try {
        stmt.execute("TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK");
        connection.commit();
      } finally {
        stmt.close();
      }
    } catch (SQLException e) {
        connection.rollback();
        throw new Exception(e);
    }
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

According to documentation at
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_truncate_statement

If RESTART IDENTITY is specified, all table IDENTITY sequences and all
SEQUENCE objects in the schema are reset to their start values

扮仙女 2024-10-24 07:03:53

我们在所有测试中所做的就是在执行最后(所有断言都通过之后)回滚事务。我们使用 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.

メ斷腸人バ 2024-10-24 07:03:53

“在测试之间清除数据库”中列出了另一个解决方案 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文