用弹簧对hibernate daos进行单元测试

时间:2022-09-19 23:26:57

I like to write JUnits for my hibernate dao implementations and seek opinion on the suggested approach for writing these unit testcases. I can think of two strategies.

我喜欢为我的hibernate dao实现编写JUnits,并就编写这些单元测试用例的建议方法征求意见。我可以想到两个策略。

  • Mocking hibernate template using a library like EasyMock and testing just the DAO implementation against these mock objects. (Not really satisfying as I would be testing against a mock layer and not really against test data)

    使用像EasyMock这样的库来模拟hibernate模板,并针对这些模拟对象测试DAO实现。 (不太满意,因为我将测试模拟层而不是真正反对测试数据)

  • Testing against a real test database (an in-memory/external) by writing some test data before running my unit test.

    在运行我的单元测试之前,通过编写一些测试数据来测试真实的测试数据库(内存/外部)。

Which approach is a good way of ensuring our DAOs are properly tested. Please point me to any examples on configuring tests using the second approach. I tried looking around but haven't found the right ones.

哪种方法是确保我们的DAO得到适当测试的好方法。请指出使用第二种方法配置测试的任何示例。我试着环顾四周但却找不到合适的。

Thanks, Siva.

谢谢,西瓦。

2 个解决方案

#1


5  

I would follow the second way, using HSQLDB as the DB engine. I think that invoking the real implementation behind a DAO has the positive effect of catching mapping errors.

我会按照第二种方式,使用HSQLDB作为数据库引擎。我认为调用DAO背后的实际实现具有捕获映射错误的积极效果。

If your DAOs have more logic that it's not related to deal with hibernate (imagine if you DAO loads some objects and then performs some operations on them to return a different object), I would create a different test class to test the methods with extra logic, and mock the methods that return the data. This allows you to set up the data in an easier way rather than priming the DB and immediately loading those objects.

如果你的DAO有更多的逻辑,它与处理hibernate无关(想象一下如果DAO加载一些对象,然后对它们执行一些操作以返回不同的对象),我会创建一个不同的测试类来测试具有额外逻辑的方法,并模拟返回数据的方法。这允许您以更简单的方式设置数据,而不是启动数据库并立即加载这些对象。

#2


3  

Test against a real database. Most of the complexity of Hibernate is in the mapping, and if you mock the SessionFactory (or what encapsulates it), you miss testing that entirely. Use the Spring Test Framework, to greatly ease your testing, and for local, "unit" tests, test against an in-memory database. H2 is simple to use and very fast (better than HSQLDB or Derby). E.g.:

针对真实数据库进行测试。 Hibernate的大部分复杂性都在于映射,如果你模拟SessionFactory(或封装它的东西),你就会完全错过测试。使用Spring Test Framework,可以极大地简化测试,并针对本地“单元”测试,针对内存数据库进行测试。 H2易于使用且速度非常快(优于HSQLDB或Derby)。例如。:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("your test context.xml")
public class FooDaoTest {
    @Autowired
    private FooDao dao;

    @Transactional
    public void saveFoo_succeeds() {
        // test save
    }

    @Transactional
    public void saveAndLoadFoo_resultsInSameFieldValues() {
        // save and load; compare fields from before to after
    }

    // test custom queries
}

#1


5  

I would follow the second way, using HSQLDB as the DB engine. I think that invoking the real implementation behind a DAO has the positive effect of catching mapping errors.

我会按照第二种方式,使用HSQLDB作为数据库引擎。我认为调用DAO背后的实际实现具有捕获映射错误的积极效果。

If your DAOs have more logic that it's not related to deal with hibernate (imagine if you DAO loads some objects and then performs some operations on them to return a different object), I would create a different test class to test the methods with extra logic, and mock the methods that return the data. This allows you to set up the data in an easier way rather than priming the DB and immediately loading those objects.

如果你的DAO有更多的逻辑,它与处理hibernate无关(想象一下如果DAO加载一些对象,然后对它们执行一些操作以返回不同的对象),我会创建一个不同的测试类来测试具有额外逻辑的方法,并模拟返回数据的方法。这允许您以更简单的方式设置数据,而不是启动数据库并立即加载这些对象。

#2


3  

Test against a real database. Most of the complexity of Hibernate is in the mapping, and if you mock the SessionFactory (or what encapsulates it), you miss testing that entirely. Use the Spring Test Framework, to greatly ease your testing, and for local, "unit" tests, test against an in-memory database. H2 is simple to use and very fast (better than HSQLDB or Derby). E.g.:

针对真实数据库进行测试。 Hibernate的大部分复杂性都在于映射,如果你模拟SessionFactory(或封装它的东西),你就会完全错过测试。使用Spring Test Framework,可以极大地简化测试,并针对本地“单元”测试,针对内存数据库进行测试。 H2易于使用且速度非常快(优于HSQLDB或Derby)。例如。:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("your test context.xml")
public class FooDaoTest {
    @Autowired
    private FooDao dao;

    @Transactional
    public void saveFoo_succeeds() {
        // test save
    }

    @Transactional
    public void saveAndLoadFoo_resultsInSameFieldValues() {
        // save and load; compare fields from before to after
    }

    // test custom queries
}