在每个测试用例之后清除内存数据库 [英] Clear the in memory database after every testcase

查看:206
本文介绍了在每个测试用例之后清除内存数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢

解决方案

如果在单元中使用 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拆卸方法执行任何设置活动.后者表明您正在用一种正在测试的方法创建新的数据库对象,恕我直言,IMHO并不完全容易将其用于可测试的行为.此外,如果要在测试后进行清理,以确保第二项测试正确运行,那么任何此类清理实际上都是第二项测试设置的一部分,而不是第一项测试的拆除.

使用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框架甚至是您的自定义持久层,则必须采用类似的方法.

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

解决方案

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.

这篇关于在每个测试用例之后清除内存数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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