【问题标题】:junit test case for a method with hibernate fetch具有休眠获取的方法的junit测试用例
【发布时间】:2012-02-24 19:58:50
【问题描述】:

我有一个User 表,在UserController.java 我有一个方法userList();,它使用休眠返回数据库中的所有用户。

如何为这段代码编写JUnit 测试用例。我是 JUnit 的新手。

请帮忙。

public List<User> userList() {      
    try
    {       
          List <User> result =  hibernateTemplate.find("from User");        
          return result;
    }
    finally { 
        //close the session and user-supplied JDBC connection 
    }
}

【问题讨论】:

    标签: java hibernate junit4


    【解决方案1】:

    我建议使用spring-test,我在做数据库测试时一直发现它非常有用。

    只需设置一个内存数据库或一个干净的数据库(无数据),仅用于测试。

    连接到它并使用@Rollback spring 注释设置您的测试。

    这指示框架在完成后回滚测试中完成的所有更改。如果您的应用程序具有正常的事务行为,这在 90% 的时间都有效,否则您可能需要编写一个 @After 方法来为您清理数据库。

    只需插入几个用户,进行检索并断言检索到的用户在数量和属性上与您插入的用户相同。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 2020-05-29
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2022-10-02
    • 1970-01-01
    相关资源
    最近更新 更多