0

我是春天的新手。

过去当我使用hibernate时。我可以轻松地测试我的 dao,因为它会自动获取 sessionFactory。

但是当我只使用 Spring(@Repositry 和 @Entity)时。

我似乎无法对我的代码进行单元测试,因为 .getHibernateTemplate() 保持返回 null。

我怀疑 hibernateSession 没有初始化。

我可以知道 Spring,如何对数据库进行单元测试?

4

1 回答 1

0

没有任何代码示例,我只能推测。但是,要测试您的存储库/实体,您的单元测试类需要初始化 spring 上下文。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:META-INF/test-context.xml")
public class YourTestClass {
    @Autowired
    <<autowire your repository/sessionFactory>> 

}

仅供参考:test-context.xml 假设您已经通过 xml 而不是 JavaConfig 配置了 spring bean。

于 2013-07-31T08:05:15.953 回答