Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Spring类编写Junit_Spring_Spring Mvc_Junit - Fatal编程技术网

如何为Spring类编写Junit

如何为Spring类编写Junit,spring,spring-mvc,junit,Spring,Spring Mvc,Junit,如何在没有任何数据库依赖的情况下对Spring类执行Junit测试 例如,如果我想为一个服务类(自动相关Dao)编写junit测试用例,那么数据源将被实例化为@autowired类 在测试过程中,是否有任何方法可以使用其他组件作为junit期间的内存/临时数据库?可以选择使用嵌入式数据库作为数据源: <jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classp

如何在没有任何数据库依赖的情况下对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>

Doc link:

Google“mocking frameworks”,我个人使用Java,但还有其他选择。您的用例可能需要模拟DAO,并让它们返回您想要的任何测试数据。