系列六、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/167625.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/167625.shtml
英文地址,請注明出處:http://en.pswp.cn/news/167625.shtml

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

相關文章

java反序列化漏洞詳解

java反序列化漏洞 文章目錄 java反序列化漏洞漏洞原理漏洞評級漏洞危害漏洞驗證漏洞防御典型案例 漏洞原理 由于java開發人員在編寫代碼時重寫了 readObject 方法&#xff0c;在重寫的 readObject 方法中調用其他函數實現鏈式調用最終調用到了危險函數&#xff0c;從而形成反序…

【C++】泛型編程 ? ( 類模板示例 - 數組類模板 | 自定義類中持有指針成員變量 )

文章目錄 一、支持 數組類模板 存儲的 自定義類1、可拷貝和可打印的自定義類2、改進方向3、改進方向 - 構造函數4、改進方向 - 析構函數5、改進方向 - 重載左移運算符6、改進方向 - 重載拷貝構造函數 和 等號運算符 二、代碼示例1、Array.h 頭文件2、Array.cpp 代碼文件3、Test…

[網鼎杯 2020 朱雀組]phpweb

看一下源碼 應該是輸入的date 作為函數&#xff0c;value作為內部參數的值&#xff0c;將date()函數返回的結果顯示在頁面上 回去看的時候&#xff0c;意外發現頁面有了新的跳轉&#xff0c;觀察一下發現&#xff0c;頁面每隔五秒就會發生一次跳轉 所以就抓包看看 抓包發現po…

GEE:kNN(k-最近鄰)分類教程(樣本制作、特征添加、訓練、精度、最優參數、統計面積)

作者:CSDN @ _養樂多_ 本文將介紹在Google Earth Engine (GEE)平臺上進行kNN(k-最近鄰)分類的方法和代碼,其中包括制作樣本點教程(本地、在線和本地在線混合制作樣本點,合并樣本點等),加入特征變量(各種指數、紋理特征、時間序列特征、物候特征等),運行kNN(k-最近…

Linux中,查看Tomcat版本、如何查看Tomcat版本

方法 在tomcat的bin目錄下&#xff0c;執行version.sh命令即可 結果

python每日一題——3最長連續序列

題目 給定一個未排序的整數數組 nums &#xff0c;找出數字連續的最長序列&#xff08;不要求序列元素在原數組中連續&#xff09;的長度。 請你設計并實現時間復雜度為 O(n) 的算法解決此問題。 示例 1&#xff1a; 輸入&#xff1a;nums [100,4,200,1,3,2] 輸出&#xf…

RpcServiceContext上下文

消費者: web 提供者: buss-service 同一服務器: 192.168.100.228 RpcServiceContext serviceContext RpcContext.getServiceContext(); //web->buss-serviceLOGGER.warn("getRequest->{}", JsonUtil.toJson(serviceContext.getRequest())); //getRequest-…

ElementUI table+dialog實現一個簡單的可編輯的表格

table組件如何實現可編輯呢&#xff1f; 我的需求是把table組件那樣的表格&#xff0c;實現它點擊可以彈出一個框&#xff0c;然后在這個框里面輸入你的東西&#xff0c;然后將他回顯回去&#xff0c;當然&#xff0c;輸入的有可能是時間啥的。 為什么要彈出彈層不在框上直接…

最近iphone手機的交管12123閃退,打不開的解決辦法?

蘋果手機系統和新版軟件不配&#xff0c;終極決絕辦法&#xff1a;升級IOS系統就好 可能是手機的內存不足了&#xff0c;因為在使用APP時&#xff0c;需要占用手機的內存&#xff0c;如果手機內存不足以支持軟件允許&#xff0c;軟件就會閃退。車主可以清理一下手機的內存&…

彈窗msvcp140_1.dll丟失的解決方法,超簡單的方法分享

在計算機使用過程中&#xff0c;我們經常會遇到一些錯誤提示&#xff0c;其中最常見的就是缺少某個文件的錯誤。最近&#xff0c;我在使用某些軟件時&#xff0c;遇到了一個名為“msvcp140_1.dll”的錯誤提示。這個錯誤通常出現在運行某些程序時&#xff0c;由于缺少了msvcp140…

項目總結報告(案例模板)

軟件項目總結報告模板套用&#xff1a; 項目概要項目工作分析經驗與教訓改進建議可納入的項目過程資產 --------進主頁獲取更多資料-------

2023年【汽車駕駛員(中級)】最新解析及汽車駕駛員(中級)試題及解析

題庫來源&#xff1a;安全生產模擬考試一點通公眾號小程序 2023年汽車駕駛員&#xff08;中級&#xff09;最新解析為正在備考汽車駕駛員&#xff08;中級&#xff09;操作證的學員準備的理論考試專題&#xff0c;每個月更新的汽車駕駛員&#xff08;中級&#xff09;試題及解…

Doris中的物化視圖-查詢(十九)

物化視圖創建完成后&#xff0c;用戶的查詢會根據規則自動匹配到最優的物化視圖。 比如我們有一張銷售記錄明細表&#xff0c;并且在這個明細表上創建了三張物化視圖。一個存儲了不同時間不同銷售員的售賣量&#xff0c;一個存儲了不同時間不同門店的銷售量&#xff0c;以及每…

C#,《小白學程序》第二課:數組,循環與排序

1 什么是數組&#xff1f; 數組 Array 是一組數值&#xff08;數 或 值&#xff09;。 int[] a; int[,] b; int[][] c; Anything[] d; 都是數組。 2 排序 排序就是按大小、名字、拼音或你指定的信息進行比較后排隊。 排序是數組最基本的功能需求。 3 文本格式 /// <summa…

《數據結構、算法與應用C++語言描述》-代碼實現散列表(線性探查與鏈式散列)

散列表 完整可編譯運行代碼&#xff1a;Github:Data-Structures-Algorithms-and-Applications/_22hash/ 定義 字典的另一種表示方法是散列&#xff08;hashing&#xff09;。它用一個散列函數&#xff08;也稱哈希函數&#xff09;把字典的數對映射到一個散列表&#xff08…

spring-webflux的一些概念的理解

Spring5的webflux可以支持高吞吐量&#xff0c;使用相同的資源可以處理更加多的請求&#xff0c;它將會成為未來技術的趨勢&#xff0c;但是相對于學習其他的框架相比&#xff0c;它的學習曲線很高&#xff0c;綜合了很多現有的技術&#xff0c;即使按照教程學習能編寫代碼&…

requests庫的學習(詳細篇)

一、request庫的安裝 requests屬于第三方庫&#xff0c;Python不內置&#xff0c;因此需要我們手動安裝。 pip3 install requests

HTML5新特性

HTML5新特性 前言語義化標簽常用語義化標簽優點 新增input屬性新增type屬性值內容其他新增input屬性 video&#xff08;視頻&#xff09;與audio&#xff08;音頻&#xff09;標簽 前言 本文主要講解HTML5中新增了哪些內容。 語義化標簽 HTML5新增了語義化標簽這個特性&…

第一類曲線積分@對弧長的曲線積分

文章目錄 abstract對弧長的曲線積分曲線形構件的質量第一類曲線積分曲線積分存在性利用曲線積分的定義描述曲線形構件質量問題推廣曲線積分可加性閉曲線積分 曲線積分性質曲線積分的計算方法證明(部分推導) 小結曲線弧顯函數形式方程下的曲線積分公式推廣例例例 abstract 在積…

html table樣式的設計 表格邊框修飾

<!DOCTYPE html> <html> <head> <meta http-equiv"Content-Type" content"text/html; charsetutf-8" /> <title>今日小說排行榜</title> <style> table {border-collapse: collapse;border: 4px double red; /*…