一:加入依賴
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>4.12</version> 5 <scope>test</scope> 6 </dependency>
二:
假設我們要對Mapper做測試,在將鼠標放在類名上使用快捷鍵 ALT + ENTER,選擇Create Test,或者 在類中鼠標右鍵,選Go To都行
OK
然后會發現,生成的測試類在 src/test
目錄下,測試類和源代碼的包名 是一致的。
三:添加SpringBoot的注釋:
1 import org.junit.Test; 2 import org.junit.runner.RunWith; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.boot.test.context.SpringBootTest; 5 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 6 7 import static org.junit.Assert.*; 8 @RunWith(SpringJUnit4ClassRunner.class) 9 @SpringBootTest 10 public class UserMapperTest { 11 @Autowired 12 private UserMapper userMapper; 13 @Test 14 public void test(){ 15 16 } 17 }
四:具體使用就和junit一樣的了,運行時,點右邊的綠色小三角就行
五:Junit提供參數化測試,套件測試等多種使用的功能。