前言
我们在写自己的单测时,通常写法都是
main方法或者SpringBootTest + junit
但是这两种写法都有问题
1、main方法,无法进行bean注入,只能进行简单的逻辑测试
2、springbootTest + junit 可以实现bean注入,但是每次测试都需要启动服务,且如果本地环境无法连接数据库,代码中走到DAO层会报错
解决方案 Mockito
当我们不想每次测试都启动服务,但是代码中又有一些数据库操作,或者其他RPC调用,想跳过这些,直接测后边的代码
pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>