0
如何在没有任何数据库依赖关系的情况下对Spring类执行Junit测试。如何为Spring类执行Junit
例如,如果我想为服务类,自动依赖Dao编写junit测试用例,那么数据源将被实例化,因为这些类都是@autowired。
有没有什么办法在测试过程中使用其他组件,这些组件在junit期间将作为内存/临时数据库使用?
如何在没有任何数据库依赖关系的情况下对Spring类执行Junit测试。如何为Spring类执行Junit
例如,如果我想为服务类,自动依赖Dao编写junit测试用例,那么数据源将被实例化,因为这些类都是@autowired。
有没有什么办法在测试过程中使用其他组件,这些组件在junit期间将作为内存/临时数据库使用?
还有就是用嵌入式数据库作为数据源的一个选项:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql"/>
</jdbc:embedded-database>
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:initialize-database>
谷歌 “嘲讽框架”,我个人使用[的Mockito(HTTP://代码。 google.com/p/mockito/)for Java,但还有其他选择。你的用例可能会涉及嘲笑你的DAO,并让它们返回你想要的任何测试数据。 – m4rtin 2014-09-13 13:13:33