1. 應用測試的介紹
一般我們在寫完代碼之后,代碼的測試是會給專門的測試人員來測試的,如果一個測試跑到你的工位上對你說,你的代碼好像有Bug,你肯定會不爽,反正我就是這樣的🙃。所以為了顯示自己的代碼質量高一點,在功能提交給測試之前,我們會自己測試一下,接下來給大家介紹一下 Spring Boot Test 應用測試框架。
Spring Boot Test 其實就是Spring Test,只是在Spring Boot 中集成變得更簡單了,像我們開發自己測試,一般都是單元測試Junit測試,不出bug就謝天謝地啦,Spring Test與JUnit結合起來提供了高效便捷的測試解決方案,而Spring Boot Test是在Spring Test之上增加了切片測試並增強了Mock能力。
Spring Boot Test支持的測試種類,主要分為以下三類:
-
單元測試,面向方法的測試,常用注解有@Test。(一般都是用這個)
-
功能測試,面向業務的測試,同時也可以使用切面測試中的Mock能力,常用的注解有@RunWith,@SpringBootTest等。(這個也用得多)
-
切片測試,面向難於測試的邊界功能,介於單元測試和功能測試之間,常用注解有@RunWith,@WebMvcTest等。
測試過程中的關鍵要素及支撐方式如下:
- 測試運行環境,通過@RunWith和@SpringBootTest啟動Spring容器。
- Mock能力,Mockito提供Mock能力。
- 斷言能力,AssertJ、Hamcrest、JsonPath提供斷言能力。
接下來我帶領大家學習如何簡單使用Spring Boot Test框架。
2. Spring Boot Test 的使用
- 2.1 引入依賴
在Spring Boot中開啟測試只需要引入spring-boot-starter-test依賴,使用@RunWith和@SpringBootTest注解就可以開始測試。我們就簡單測試一下接口,首先我們引入pom依賴:
pom.xml
:
<!--springboot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--springboot框架web組件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--mybatis整合springboot組件-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--mysql數據庫連接驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!--lombok組件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!--spring boot-test組件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!--單元測試junit組件-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring-test組件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--springboot的maven插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
-
2.2 代碼編寫
測試代碼我們一般都寫在與main文件夾平級的test文件夾下,建議文件夾的名稱和main文件夾下的文件夾對應好,測試類的名稱也要對應好,就像下面這樣,當然這只是建議。
Spring Boot 的啟動類我就不再寫了,沒有加什么特別的內容,直接上測試類吧。
StudentServiceTest.java
:對了,這里測試的每一個方法都和
StudentService.java
這個服務類的方法名對應着的,建議大家也是這樣。package com.butterflytri.service; import com.butterflytri.TestApplication; import com.butterflytri.entity.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.List; /** * @author: WJF * @date: 2020/5/23 * @description: StudentServiceTest */ /** * {@link SpringBootTest}:配置文件屬性的讀取。讀取classes標志的啟動類的配置文件和運行環境,並加載。 * {@link RunWith}:'RunWith'注解就是一個運行器,加載value的Class測試環境。 */ @SpringBootTest(classes = TestApplication.class) @RunWith(SpringRunner.class) public class StudentServiceTest { @Resource private StudentService studentService; @Test public void findAllTest() { List<Student> list = studentService.findAll(); for (Student student : list) { System.out.println(student); } } @Test public void findOneTest() { Student student = studentService.findOne(1L); System.out.println(student); } @Test public void findByStudentNoTest() { Student student = studentService.findByStudentNo("G030511"); System.out.println(student); } }
每一個測試方法都是可以獨立運行的,因為在運行的時候會加載Spring Test的測試環境,同時會將你測試的工程的運行環境的配置加載進來,使用的都是工程的配置和環境,方便我們自己進行測試。
3. 項目地址
本項目傳送門:
- GitHub ---> spring-boot-test
- Gitee ---> spring-boot-test
此教程會一直更新下去,覺得博主寫的可以的話,關注一下,也可以更方便下次來學習。
- 作者:Butterfly-Tri
- 出處:Butterfly-Tri個人博客
- 版權所有,歡迎保留原文鏈接進行轉載🙃