前言??
??????SSM專欄更新中,各位大佬覺得寫得不錯(cuò),支持一下,感謝了!??????
Spring + Spring MVC + MyBatis_冷兮雪的博客-CSDN博客
Spring Boot 中進(jìn)行單元測試是一個(gè)常見的做法,可以幫助你驗(yàn)證應(yīng)用程序的各個(gè)組件是否按預(yù)期工作。所以我們有必要去學(xué)習(xí)一番!
一、什么是單元測試???
單元測試(unit testing),是指對軟件中的最小可測試單元進(jìn)行檢查和驗(yàn)證的過程就叫單元測試。單元測試是開發(fā)者編寫的一小段代碼,用于檢驗(yàn)被測代碼的?個(gè)很小的、很明確的(代碼)功能是否正確。執(zhí)行單元測試就是為了證明某段代碼的執(zhí)行結(jié)果是否符合我們的預(yù)期。如果測試結(jié)果符合我們的預(yù)期,稱之為測試通過,否則就是測試未通過(或者叫測試失?。?/span>
二、單元測試有哪些好處???
- 方便、快捷測試一個(gè)功能模塊(方法級別)
- 在打包時(shí)會(huì)運(yùn)行所以的單元測試,只有所有的單元測試都通過之后才能正常的打包,所以在這個(gè)過程中可以幫我們發(fā)現(xiàn)一些問題,減少問題的概率。
- 使用單元測試可以在不污染數(shù)據(jù)庫數(shù)據(jù)的情況下,來測試某個(gè)功能的正確性。(添加@Transactional注解即可)MyBatis查詢數(shù)據(jù)庫(2)_冷兮雪的博客-CSDN博客
三、Spring Boot 單元測試使用??
Spring Boot 項(xiàng)目創(chuàng)建時(shí)會(huì)默認(rèn)單元測試框架 spring-boot-test,而這個(gè)單元測試框架主要是依靠另? 個(gè)著名的測試框架 JUnit 實(shí)現(xiàn)的,打開 pom.xml 就可以看到,以下信息是 Spring Boot 項(xiàng)目創(chuàng)建是自動(dòng)添加的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
而 spring-boot-starter-test 的 MANIFEST.MF(Manifest 文件是用來定義擴(kuò)展或檔案打包的相關(guān)信息的)里面有具體的說明,如下信息所示:
四、單元測試的實(shí)現(xiàn)步驟??
1.生成單元測試類??
在需要進(jìn)行單元測試的類里面右鍵:
?UserMapperTest生成在text目錄下:
package com.example.ssmdemo1.mapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class UserMapperTest {
@Test
void getUserById() {
}
}
這個(gè)時(shí)候,此方法是不能調(diào)用到任何單元測試的方法的,此類只生成了單元測試的框架類,具體的業(yè)務(wù)代碼要自己填充。
2、添加單元測試代碼??
Ⅰ、添加 Spring Boot 框架測試注解:@SpringBootTest??
package com.example.ssmdemo1.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest//表明當(dāng)前單元測試是運(yùn)行在Spring Boot環(huán)境中的
class UserMapperTest {
@Test
void getUserById() {
}
}
Ⅱ、添加單元測試業(yè)務(wù)邏輯??
package com.example.ssmdemo1.mapper;
import com.example.ssmdemo1.entity.Userinfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest//1、表明當(dāng)前單元測試是運(yùn)行在Spring Boot環(huán)境中的
class UserMapperTest {
//2、注入測試對象
@Autowired
private UserMapper userMapper;
@Test
void getUserById() {
//3、添加單元測試的業(yè)務(wù)代碼
Userinfo userinfo=userMapper.getUserById(1);
System.out.println(userinfo);
}
}
啟動(dòng)測試項(xiàng)目:
我們進(jìn)行單元測試, 后面需要去運(yùn)行我們的項(xiàng)目,我們一定要將右上角重新切換過來:
五、簡單的斷言說明??
方法 | 說明 |
assertEquals | 判斷兩個(gè)對象或兩個(gè)原始類型是否相等 |
assertNotEquals | 判斷兩個(gè)對象或兩個(gè)原始類型是否不相等 |
assertSame | 判斷兩個(gè)對象引用是否指向同一個(gè)對象 |
assertNotSame | 判斷兩個(gè)對象引用是否指向不同的對象 |
assertTrue | 判斷給定的布爾值是否為 true |
assertFalse | 判斷給定的布爾值是否為 false |
assertNull | 判斷給定的對象引用是否為 nul |
assertNotNull | 判斷給定的對象用是否不為 null |
斷言:如果斷言失敗,則后面的代碼都不會(huì)執(zhí)行。
package com.example.ssmdemo1.mapper;
import com.example.ssmdemo1.entity.Userinfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest//1、表明當(dāng)前單元測試是運(yùn)行在Spring Boot環(huán)境中的
class UserMapperTest {
//2、注入測試對象
@Autowired
private UserMapper userMapper;
@Test
void getUserById() {
//3、添加單元測試的業(yè)務(wù)代碼
Userinfo userinfo=userMapper.getUserById(1);
System.out.println(userinfo);
//判斷1是否等于2 簡單斷言
Assertions.assertEquals(1,2);
}
}
?單元測試失?。?/p>
單元測試失敗,這時(shí)候我們?nèi)ゴ虬矔?huì)打包失?。?/p>
?打包失?。?img src="https://imgs.yssmx.com/Uploads/2023/07/547233-11.png" alt="Spring Boot單元測試,Spring + Spring MVC + MyBatis,spring boot,單元測試,后端" referrerpolicy="no-referrer" />
?打包成功:
文章來源:http://www.zghlxwxcb.cn/news/detail-547233.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-547233.html
到了這里,關(guān)于Spring Boot單元測試的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!