让 DbUnit 与 Hibernate 事务一起工作

发布于 2024-12-22 05:28:34 字数 2276 浏览 1 评论 0原文

我在尝试将 Hibernate 事务中所做的更改推送到数据库以使 DbUnit 在我的测试用例中正常工作时遇到问题。看起来 DbUnit 没有看到 Hibernate 所做的更改,因为它们尚未在事务结束时提交......并且我不确定如何重组我的测试用例以使其正常工作。

这是我过度简化的测试用例来演示我的问题:-

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:applicationContext-test.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class SomeTest {
    @Autowired
    protected DataSource dataSource;

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    public void testThis() throws Exception {
        Session session = sessionFactory.getCurrentSession();

        assertEquals("initial overlayType count", 4, session.createQuery("from OverlayType").list().size());

        //-----------
        // Imagine this block is an API call, ex: someService.save("AAA");
        // But for the sake of simplicity, I do it this way
        OverlayType overlayType = new OverlayType();
        overlayType.setName("AAA");
        session.save(overlayType);
        //-----------

        // flush has no effect here
        session.flush();

        assertEquals("new overlayType count", 5, session.createQuery("from OverlayType").list().size());

        // pull the data from database using dbunit
        IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        partialDataSet.addTable("resultSet", "select * from overlayType");
        ITable actualTable = partialDataSet.getTable("resultSet");

        // FAIL: Actual row count is 4 instead of 5
        assertEquals("dbunit's overlayType count", 5, actualTable.getRowCount());

        DataSourceUtils.releaseConnection(connection.getConnection(), dataSource);
    }
}

我使用 DbUnit 的整个想法是:-

  • 调用将数据保存到多个表中的 someService.save(...)
  • 使用 DbUnit 从 XML 中获取所需的表。
  • 使用 DbUnit 从数据库获取实际表。
  • 执行Assertion.assertEquals(expectedTable,actualTable);。

但是,此时,我无法让 DbUnit 查看 Hibernate 在事务中所做的更改。

我应该如何更改才能使 DbUnit 与 Hibernate 事务良好配合?

谢谢。

I'm having problem trying to push changes made within a Hibernate transaction to the database for DbUnit to work properly in my test case. It seems like DbUnit is not seeing the changes made by Hibernate because they are not committed at the end of the transaction yet... and I'm not sure how to restructure my test case to get this to work.

Here's my over-simplified test case to demonstrate my problem:-

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:applicationContext-test.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class SomeTest {
    @Autowired
    protected DataSource dataSource;

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    public void testThis() throws Exception {
        Session session = sessionFactory.getCurrentSession();

        assertEquals("initial overlayType count", 4, session.createQuery("from OverlayType").list().size());

        //-----------
        // Imagine this block is an API call, ex: someService.save("AAA");
        // But for the sake of simplicity, I do it this way
        OverlayType overlayType = new OverlayType();
        overlayType.setName("AAA");
        session.save(overlayType);
        //-----------

        // flush has no effect here
        session.flush();

        assertEquals("new overlayType count", 5, session.createQuery("from OverlayType").list().size());

        // pull the data from database using dbunit
        IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        partialDataSet.addTable("resultSet", "select * from overlayType");
        ITable actualTable = partialDataSet.getTable("resultSet");

        // FAIL: Actual row count is 4 instead of 5
        assertEquals("dbunit's overlayType count", 5, actualTable.getRowCount());

        DataSourceUtils.releaseConnection(connection.getConnection(), dataSource);
    }
}

My whole idea in using DbUnit is to:-

  • Call someService.save(...) that saves data into several tables.
  • Use DbUnit to get expected table from XML.
  • Use DbUnit to get actual table from database.
  • Do Assertion.assertEquals(expectedTable, actualTable);.

But, at this point, I'm not able to get DbUnit to see the changes made by Hibernate within the transaction.

How should I change to get DbUnit to work nicely with Hibernate transaction?

Thanks.

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

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

发布评论

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

评论(2

此刻的回忆 2024-12-29 05:28:34

我从未使用过 DbUnit,但似乎 < code>TransactionAwareDataSourceProxy 就可以了。基本上,您需要使用此代理包装原始数据源并使用它,以便此代码:

new DatabaseConnection(dataSource.getConnection())

实际上通过代理并使用与 Hibernate 相同的事务和连接。

我发现 事务感知数据源(使用 dbunit & 在春天休眠) 博客文章解释了这一点。

另一种方法是完全跳过事务测试并手动清理数据库。查看我的被认为有害的事务测试文章。

I have never worked with DbUnit, but it seems like TransactionAwareDataSourceProxy will do the trick. Basically you need to wrap your original data source with this proxy and use it instead, so that this code:

new DatabaseConnection(dataSource.getConnection())

actually goes through the proxy and uses the same transaction and connection as Hibernate.

I found Transaction aware datasource (use dbunit & hibernate in spring) blog post explaining this.

Another approach would be to skip transactional tests altogether and cleanup the database instead manually. Check out my transactional tests considered harmful artcle.

姜生凉生 2024-12-29 05:28:34

看起来该测试用例需要两个事务:一个用于将数据放入数据库,第二个用于检索数据。

我要做的是:

  • 使用内存数据库,以便在单元测试结束时清理数据。
  • 删除事务注释并直接使用会话的 beginTransaction 和 commit 方法。

初始覆盖类型计数将为 0,保存会话后,它应该为 1。

Looks like that test case needs two transactions: one for putting data into the database, and a second one to retrieve it.

What I would do is:

  • Use a memory database so the data is cleaned when the unit test ends.
  • Remove the transactional annotations and use the beginTransaction and commit methods of the session directly.

The initial overlaytype count would be 0, and after the session is saved, it should be 1.

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