每个测试用例后清除内存数据库

发布于 2024-11-27 14:12:38 字数 180 浏览 2 评论 0原文

我正在使用 hsqldb 来测试 Java 中的一些数据访问层。我有大约 100 个测试用例。我创建一个内存数据库,然后在表中插入一些值,以便通过我的测试用例我可以加载它,但问题是对于每个测试用例,我需要清除内存数据库,只有值而不是表。

是否可能,一件事是我需要手动删除表中的行,还有其他我可以使用的东西吗?

谢谢

I am using hsqldb for testing some of the data access layer in Java. I have certain test cases like 100 around. I create a in memory database and then insert some values in the table so that with my test case i can load it, but the problem is for every test case i need to clear the in memory database, only the values not the tables.

Is it possible, one thing is i need to manually delete the rows from the table and is there some thing else I can use.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

蹲在坟头点根烟 2024-12-04 14:12:38

如果您在单元测试中使用DbUnit,则可以指定DbUnit应在之前执行清理和插入操作每次测试前确保数据库内容处于有效状态。这可以通过类似于下面的方式来完成:

@Before
public void setUp() throws Exception
{
    logger.info("Performing the setup of test {}", testName.getMethodName());
    IDatabaseConnection connection = null;
    try
    {
        connection = getConnection();
        IDataSet dataSet = getDataSet();
        //The following line cleans up all DbUnit recognized tables and inserts and test data before every test.
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    }
    finally
    {
        // Closes the connection as the persistence layer gets it's connection from elsewhere
        connection.close();
    }
}

请注意,始终建议在 @Before 设置方法中执行任何设置活动,而不是在 @After 中> 拆解方法。后者表明您正在正在测试的方法中创建新的数据库对象,恕我直言,这并不容易实现可测试的行为。此外,如果您在测试后进行清理,以确保第二个测试正确运行,那么任何此类清理实际上都是第二个测试设置的一部分,而不是第一个测试的拆卸。

使用 DbUnit 的替代方法是在 @Before 设置方法中启动一个新事务,并在 @After 拆卸方法中将其回滚。这取决于您的数据访问层的编写方式。

如果您的数据访问层接受 Connection 对象,那么您的设置例程应该创建它们,并关闭自动提交。此外,还假设您的数据访问层不会调用Connection.commit。假设前面的情况,您可以在拆卸方法中使用 Connection.rollback() 回滚事务。

关于事务控制,下面的代码片段演示了如何使用 JPA 来做到这一点:

@Before
public void setUp() throws Exception
{
    logger.info("Performing the setup of test {}", testName.getMethodName());
    em = emf.createEntityManager();
    // Starts the transaction before every test
    em.getTransaction.begin();
}

@After
public void tearDown() throws Exception
{
    logger.info("Performing the teardown of test {}", testName.getMethodName());
    if (em != null)
    {
        // Rolls back the transaction after every test
        em.getTransaction().rollback();
        em.close();
    }
}

对于其他 ORM 框架甚至您的自定义持久层(如果您已经编写了一个)也必须采取类似的方法。

If you use DbUnit in unit-tests, you can specify that DbUnit should perform a clean-and-insert operation before every test to ensure that the contents of the database are in a valid state before every test. This can be done in a manner similar to the one below:

@Before
public void setUp() throws Exception
{
    logger.info("Performing the setup of test {}", testName.getMethodName());
    IDatabaseConnection connection = null;
    try
    {
        connection = getConnection();
        IDataSet dataSet = getDataSet();
        //The following line cleans up all DbUnit recognized tables and inserts and test data before every test.
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    }
    finally
    {
        // Closes the connection as the persistence layer gets it's connection from elsewhere
        connection.close();
    }
}

Note that it is always recommended to perform any setup activities in a @Before setup method, rather than in a @After teardown method. The latter indicates that you are creating new database objects in a method being tested, which IMHO does not exactly lend easily to testable behavior. Besides, if you are cleaning up after a test, to ensure that a second test runs correctly, then any such cleanup is actually a part of the setup of the second test, and not a teardown of the first.

The alternative to using DbUnit is to start a new transaction in your @Before setup method, and to roll it back in the @After teardown method. This would depend on how your data access layer is written.

If your data access layer accepts Connection objects, then your setup routine should create them, and turn off auto-commit. Also, there is an assumption that your data access layer will not invoke Connection.commit. Assuming the previous, you can rollback the transaction using Connection.rollback() in your teardown method.

With respect to transaction control, the below snippet demonstrates how one would do it using JPA for instance:

@Before
public void setUp() throws Exception
{
    logger.info("Performing the setup of test {}", testName.getMethodName());
    em = emf.createEntityManager();
    // Starts the transaction before every test
    em.getTransaction.begin();
}

@After
public void tearDown() throws Exception
{
    logger.info("Performing the teardown of test {}", testName.getMethodName());
    if (em != null)
    {
        // Rolls back the transaction after every test
        em.getTransaction().rollback();
        em.close();
    }
}

Similar approaches would have to be undertaken for other ORM frameworks or even your custom persistence layer, if you have written one.

浅紫色的梦幻 2024-12-04 14:12:38

您可以使用HSQLDB事务吗?

在每次测试之前,启动一个新事务:

START TRANSACTION;

在每次测试之后,将其回滚:

ROLLBACK;

这也将允许您拥有一些永久数据。

Could you use HSQLDB transactions?

Before every test, start a new transaction:

START TRANSACTION;

After every test, roll it back:

ROLLBACK;

This would also allow you to have some permanent data.

梦巷 2024-12-04 14:12:38

根据您的测试框架,可以在每次测试后执行删除调用。在 Junit 中,注释是 @After 并且带有此注释的方法将是在每个 [@Test] 方法之后运行。

Depending on your test framework, it is possible to execute a delete call after each test. In Junit the annotation is @After and a method with this annotation will be run after each [@Test] method.

旧故 2024-12-04 14:12:38

您必须使用截断查询来销毁数据库内存,否则此链接会对您有所帮助。

http://wiki.apache.org/db-derby/InMemoryBackEndPrimer

You have to use Truncate Query for the Destroy Database Memory or this link can be helpful to you.

http://wiki.apache.org/db-derby/InMemoryBackEndPrimer

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