前言
本文使用的項目代碼,是基于之前文章使用的Spring Boot項目:
單元測試步驟
1. 添加依賴
2. 創建單元測試類
3. 編寫單元測試類
4. 運行單元測試
5. 踩坑
1. 添加依賴;
//在pom.xml文件中的dependencies節點內添加以下內容即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2. 創建單元測試類;
單元測試類
package com.mycompany.sample;
/**
* @author : dylanz
* @since : 07/24/2020
**/
public class LeadServiceTest {
}
3. 編寫單元測試類;
package com.mycompany.sample;
import com.mycompany.sample.domain.Lead;
import com.mycompany.sample.service.LeadService;
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author : dylanz
* @since : 07/24/2020
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class LeadServiceTest {
@Autowired
private LeadService leadService;
@BeforeClass
public static void beforeClassTest() {
System.out.println("This is beforeClassTest");
System.out.println("-----------------------");
}
@Before
public void beforeTest() {
System.out.println("This is beforeTest");
System.out.println("-----------------------");
}
@Test
public void getLeadByLeadIdTest() {
Long leadId = 10130546L;
Lead lead = leadService.getLeadByLeadId(leadId);
//為了驗證leadService.getLeadByLeadId()是否被調用成功,我們把結果打印出來:
System.out.println("leadId:" + lead.leadId);
System.out.println("email:" + lead.email);
Assert.assertEquals(leadId, lead.leadId);
Assert.assertNotNull(lead.email);
}
@After
public void afterTest() {
System.out.println("This is afterTest");
System.out.println("-----------------------");
}
@AfterClass
public static void afterClassTest() {
System.out.println("This is afterClassTest");
System.out.println("-----------------------");
}
}
關于測試類,主要關注以下幾點:
1. 在測試類中,應使用對應注解:@RunWith(SpringRunner.class)和@SpringBootTest;
2. BeforeClass和AfterClass方法必須為static類型;
3. BeforeClass比Before運行早,AfterClass比After運行晚,整體順序為:
BeforeClass >>> Before >>> Test >>> After >>> AfterClass
4. 運行單元測試;
運行單元測試
運行結果
5. 踩坑;
至此,我們已經能夠在Spring Boot框架下開始使用junit進行基本的單元測試,但故事往往沒有這么單純順利,其實在運行的初期,我遇到了一個問題:
在運行單元測試時,控制臺有報錯,錯誤如下:
報錯1
經過一番搜索查閱,網上資料說是框架中自動使用了junit5,需要更改pom.xml, 忽略5.x的junit相關jar(因為我使用的是junit 4.1.3):
5.x junit jar
于是在spring-boot-starter-test依賴內忽略他們,如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
修改完成后,再次運行,結果又換了個報錯:
報錯2
又一臉蒙,我懷疑是不是沒忽略所有5.x,驗證后也不是這個問題。幾經搜索后發現,竟然有可能跟IntelliJ的版本有關系,并且該報錯中也確實有提到IntelliJ,升級完IntelliJ,再次運行單元測試,才最終工作正常。并且移除pom.xml中忽略的junit 5.x相關包配置,重新加載5.x的包之后,運行單元測試,工作也仍正常,因此根本原因是:
IntelliJ版本問題
(真是萬萬沒想到啊!)
出錯的版本:2016.3.4的社區版
升級后的版本:2019.1的社區版
僅以此文入手Spring Boot單元測試,并記錄實現過程中遇到的奇葩問題!
更多單元測試相關的知識未來繼續學習!!!