系列二、Spring整合單元測試

一、概述

? ? ? ? Spring中獲取bean最常見的方式是通過ClassPathXmlApplicationContext 或者 AnnotationConfigApplicationContext的getBean()方式獲取bean,那么在Spring中如何像在SpringBoot中直接一個類上添加個@SpringBootTest注解,即可在類中注入自己想要測試的bean呢?解決方案是有的,spring-test即提供了這個功能。Spring整合單元測試步驟如下:

二、Spring整合Junit單元測試

2.1、整體結構

2.2、pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.star</groupId><artifactId>spring5x06-mybatis</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>spring5x06-mybatis</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--spring基本依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.5.RELEASE</version></dependency><!-- 數據源 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.27</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.11</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.1.0</version></dependency><!-- 普通maven項目中使用Sl4j注解 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.32</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.10</version></dependency><!-- aop --><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.1</version></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.19</version></dependency><!-- 工具 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.3</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.11</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.22</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.1</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory></resource></resources></build></project>

2.3、使用

/*** @Author : 一葉浮萍歸大海* @Date: 2023/11/23 19:12* @Description: Spring整合單元測試*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {@Resourceprivate UserMapper userMapper;@Resourceprivate UserService userService;@Testpublic void userMapperTest() {List<UserDO> userDOS = userMapper.listAllUser();System.out.println("userMapper = " + userMapper);System.out.println("userDOS = " + userDOS);}@Testpublic void userServiceTest() {List<UserDO> userDOS = userService.listAllUser();System.out.println("userService = " + userService);System.out.println("userDOS = " + userDOS);}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/165301.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/165301.shtml
英文地址,請注明出處:http://en.pswp.cn/news/165301.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

優秀的企業協同OA系統如何促進團隊協作?

優秀的企業協同OA系統可以幫助團隊協同工作變得更簡單、更高效。 一、以下是一些優秀的企業協同OA系統可以幫助團隊協作的方法&#xff1a; 1、提高工作效率 優秀的企業協同OA系統可以提高工作效率&#xff0c;減少工作的重復性和低效率。例如&#xff0c;可以使用共享日歷和…

vue2生命周期

前言 vue的生命周期其實可以分為兩塊,一個是vue實例的生命周期,一個是組件的生命周期。 vue實例的生命周期方法共有4個:$mout,$forceUpdate,$nextTick,$destroy vue組件的生命周期鉤子共有8個:beforeCreate,created,beforeMount,mounted,beforeUpdate, updated,beforeDestr…

C/C++小寫字母的判斷 2022年3月電子學會中小學生軟件編程(C/C++)等級考試一級真題答案解析

目錄 C/C小寫字母的判斷 一、題目要求 1、編程實現 2、輸入輸出 二、算法分析 三、程序編寫 四、程序說明 五、運行結果 六、考點分析 C/C小寫字母的判斷 2022年3月 C/C編程等級考試一級編程題 一、題目要求 1、編程實現 輸入一個字符&#xff0c;判斷是否是英文小…

iframe內部子頁面與外部主頁面通訊

文章目錄 一、問題二、解決2.1、子頁面2.2、主頁面 三、知識點3.1、[瀏覽器兼容性](https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage#%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%BC%E5%AE%B9%E6%80%A7)3.2、詳解3.2.1、發送方3.2.2、接收方 一、問題 如上所示&a…

被刪數據庫+勒索

被刪數據庫勒索 – WhiteNights Site 標簽&#xff1a;日志 快照在手&#xff0c;天下我有 服務器又給人黑了。這次是給人黑了數據庫&#xff0c;被人刪庫勒索&#xff0c;先放圖吧。我還花了今天一個下午的時間查修這個漏洞&#xff0c;導致網站有接近6個小時都沒法訪問。 …

編程示例: Session Id的生成

實現思路 Session的實現方式如下&#xff1a;在用戶第一次登錄的時候&#xff0c;系統為它分配一個唯一Id(被稱為Session Id)作為標識&#xff0c;并且 記錄下這個用戶的用戶名、要登錄的賬套名、用戶擁有的權限等&#xff0c;以Id為鍵&#xff0c;用戶名、賬套名等信息為值保…

云原生Kubernetes系列 | Kubernetes靜態Pod的使用

云原生Kubernetes系列 | Kubernetes靜態Pod的使用 靜態pod不建議在master上操作,因為master上跑的是集群核心靜態pod,如果配置失敗,會導致集群故障。建議在knode1或knode2上去做。 kubernetes master節點上的核心組件pod其實都是靜態pod: [root@k8s-master ~]# ls /etc/ku…

java中鎖的使用-實戰

技術主題 技術原理 在實際的業務中,為了保證線程安全,會進行對一些邏輯進行加鎖,保證當前只有一個線程在操作。在代碼中,我們經常使用兩種方式進行加鎖,來保證線程安全。一種是synchronized關鍵字,另一種是ReentrantLock加鎖。寫這一份的博客,就是為了記錄下鎖的基本使…

五分鐘,Docker安裝flink,并使用flinksql消費kafka數據

1、拉取flink鏡像&#xff0c;創建網絡 docker pull flink docker network create flink-network2、創建 jobmanager # 創建 JobManager docker run \-itd \--namejobmanager \--publish 8081:8081 \--network flink-network \--env FLINK_PROPERTIES"jobmanager.rpc.ad…

Redis 命令行 redis-cli 介紹

文章目錄 前言1. 連接 Redis 服務2. 從其他程序獲取輸入3. 連續執行相同的命令4. CSV 輸出5. 輸出幫助信息6. 清空屏幕7. 連續監控統計信息8. 輸出大 key 報告9. 監控 Redis 執行的命令 前言 redis-cli 是 Redis 自帶的命令行工具&#xff0c;是運維和開發人員常用的工具&…

【C++干貨鋪】適配器 | stack | queue

個人主頁點擊直達&#xff1a;小白不是程序媛 C系列學習專欄&#xff1a;C干貨鋪 代碼倉庫&#xff1a;Gitee 目錄 stack的介紹和使用 stack的介紹 stack的使用 queue的介紹和使用 queue的介紹 queue的使用 容器適配器 什么是適配器 STL中stack和queue的底層結構 d…

Doris擴容和縮容(六)

Doris 可以很方便的擴容和縮容 FE、BE、Broker 實例。 FE 擴容和縮容 可以通過將 FE 擴容至 3 個以上節點來實現 FE 的高可用。 1&#xff09;使用 MySQL 登錄客戶端后&#xff0c;可以使用 sql 命令查看 FE 狀態&#xff0c;目前就一臺 FE mysql -h hadoop1 -P 9030 -uroo…

Python——基于YOLOV8的車牌識別(源碼+教程)

目錄 一、前言 二 、完成效果 三、 項目包 四、運行項目 &#xff08;教程&#xff09; 一、前言 YOLOv8LPRNet車牌定位與識別https://www.bilibili.com/video/BV1vk4y1E7MZ/ 最近做了有一個車牌識別的小需求&#xff0c;今天完成了&#xff0c;在此記錄和分享 首先&#x…

辦公技巧:Word中插入圖片、形狀、文本框排版技巧

目錄 一、插入圖片排版技巧 二、添加形狀排版技巧 三、插入“文本框”排版技巧 我們平常在制作word時候經常會遇到插入選項卡下的圖片、形狀和文本框這三種情況下&#xff0c;那么如何使得Word文檔當中添加這三個元素的同時&#xff0c;又能保證樣式美觀呢&#xff0c;今天小…

ComfyUI搭建使用教程

ComfyUI 是一個基于節點流程式的stable diffusion AI 繪圖工具WebUI&#xff0c; 你可以把它想象成集成了stable diffusion功能的substance designer&#xff0c; 通過將stable diffusion的流程拆分成節點&#xff0c;實現了更加精準的工作流定制和完善的可復現性。但節點式的工…

【分布式】分布式事務及其解決方案

目錄 一、分布式事務二、分布式事務的解決方案1. 全局事務&#xff08;1&#xff09;DTP模型&#xff08;2&#xff09; 兩階段提交協議&#xff08;2PC&#xff09;原理二階段提交的缺點 &#xff08;3&#xff09;三階段提交協議&#xff08;3PC&#xff09;原理 2. 基于可靠…

【算法】搭配購買(01背包,加權并查集)

題目 Joe覺得云朵很美&#xff0c;決定去山上的商店買一些云朵。 商店里有 n 朵云&#xff0c;云朵被編號為 1,2,…,n&#xff0c;并且每朵云都有一個價值。 但是商店老板跟他說&#xff0c;一些云朵要搭配來買才好&#xff0c;所以買一朵云則與這朵云有搭配的云都要買。 …

DDoS攻擊和CC攻擊有什么不同之處?

DDoS是針對服務器IP發起&#xff0c;CC攻擊針對的是業務端口。DDoS攻擊打的是網站的服務器&#xff0c;而CC攻擊是針對網站的頁面攻擊&#xff0c;用術語來說就是&#xff0c;一個是WEB網絡層拒絕服務攻擊&#xff08;DDoS&#xff09;&#xff0c;一個是WEB應用層拒絕服務攻擊…

Linux添加環境變量$PATH

變量$PATH 查看環境變量 [rootlocalhost lnserver]# echo $PATH /usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/root/bin由于沒有docker路徑的環境變量&#xff0c;docker命令使用無效 要將腳本添加到 PATH 中&#xff0c;以便無論在哪個目錄中都可以調用它或執行它&…

【鏈路追蹤】xxl-job定時任務日志增加traceId

問題背景 項目中通過sleuth實現了統一的traceId注入&#xff0c;在生產環境進行日志追溯時比較方便。但是在使用xxl-job進行定時任務管理時&#xff0c;卻發現xxl-job線程打印出來的日志沒有traceId&#xff0c;查詢日志時十分不方便&#xff0c;于是通過使用Spring aop的方式…