前言
我們在寫自己的單測時,通常寫法都是
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>