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個人博客
- 著作權所有,歡迎保留原文鏈接進行轉載??
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/177262.html
標籤:Java
下一篇:DDD之2領域概念
