spring 單元測試 Junit

我是南城余!阿里云開發者平臺專家博士證書獲得者!

歡迎關注我的博客!一同成長!

一名從事運維開發的worker,記錄分享學習。

專注于AI,運維開發,windows Linux 系統領域的分享!

本章節對應知識庫

https://www.yuque.com/nanchengcyu/java

本內容來自尚硅谷課程,此處在知識庫做了個人理解
————————————————

6、單元測試:JUnit

在之前的測試方法中,幾乎都能看到以下的兩行代碼:

ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
Xxxx xxx = context.getBean(Xxxx.class);

這兩行代碼的作用是創建Spring容器,最終獲取到對象,但是每次測試都需要重復編寫。針對上述問題,我們需要的是程序能自動幫我們創建容器。我們都知道JUnit無法知曉我們是否使用了 Spring 框架,更不用說幫我們創建 Spring 容器了。Spring提供了一個運行器,可以讀取配置文件(或注解)來創建容器。我們只需要告訴它配置文件位置就可以了。這樣一來,我們通過Spring整合JUnit可以使程序創建spring容器了

6.1、整合JUnit5

6.1.1、搭建子模塊

搭建spring-junit模塊

6.1.2、引入依賴
<dependencies><!--spring context依賴--><!--當你引入Spring Context依賴之后,表示將Spring的基礎依賴引入了--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.2</version></dependency><!--spring對junit的支持相關依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.0.2</version></dependency><!--junit5測試--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.0</version></dependency><!--log4j2的依賴--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.19.0</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j2-impl</artifactId><version>2.19.0</version></dependency>
</dependencies>
6.1.3、添加配置文件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.atguigu.spring6.bean"/>
</beans>

copy日志文件:log4j2.xml

6.1.4、添加java類
package com.atguigu.spring6.bean;import org.springframework.stereotype.Component;@Component
public class User {public User() {System.out.println("run user");}
}
6.1.5、測試
import com.atguigu.spring6.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;//兩種方式均可
//方式一
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:beans.xml")
//方式二
@SpringJUnitConfig(locations = "classpath:beans.xml")
public class SpringJUnit5Test {@Autowiredprivate User user;@Testpublic void testUser(){System.out.println(user);}
}

6.2、整合JUnit4

JUnit4在公司也會經常用到,在此也學習一下

6.2.1、添加依賴
<!-- junit測試 -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>
6.2.2、測試
import com.atguigu.spring6.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class SpringJUnit4Test {@Autowiredprivate User user;@Testpublic void testUser(){System.out.println(user);}
}

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

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

相關文章

解決打印機超出字節之使用PHP拆分字符串并保留分隔符

在開發過程中&#xff0c;我們經常需要將一個長字符串拆分成多個片段&#xff0c;并且保留原始字符串中的分隔符。在本篇教程中&#xff0c;我們將使用PHP編寫一個函數來實現這個功能。 首先&#xff0c;讓我們來看一下我們要解決的問題。假設我們有一個長字符串&#xff0c;我…

pywinrm 解析輸出為 dict 格式 字典格式

s winrm.Session(host, auth(username, password))task_folder \RPA\\ # 運行 schtasks 命令來列出任務計劃程序 command schtasks /query /fo csv # command fschtasks /query /fo csv /tn {task_folder} # command fschtasks /query /fo csv /tn "{task_folder}\\…

mapbox使用v3版本,v2的樣式切換不同時間段

創建DayAndNight.js /*** 使用方式* const dayNight new DayAndNight({ map: map // map 地圖對象}) * 修改類型* dayNight.setConfigProperty(value)*/ class DayAndNight {constructor (sdMap) {this.map sdMap.mapthis.initStyle()}// 初始化時添加必要樣式initStyle () {…

刪除計算機用戶時拒絕訪問權限,c盤為什么拒絕訪問 刪除c盤文件需要管理員權限怎么辦...

轉載&#xff1a;??????刪除計算機用戶時拒絕訪問權限,c盤為什么拒絕訪問 刪除c盤文件需要管理員權限怎么辦...-CSDN博客 c盤是電腦中的關鍵位置&#xff0c;存儲著很多系統重要文件&#xff0c;如果電腦出問題一般就是c盤中的文件異常&#xff0c;近日有小伙伴出現這樣…

前端知識筆記(四十一)———nodejs發起http或https請求

http請求 const express require(express) const http require(http)const app express()const loginConfig (token) > {return {hostname: api.test.com,port: 80,path: /test?access_token${token},method: GET} }app.get(/login, (req, res) > {const options …

c++通過serial庫進行上下位機通信

?編輯 風紊 現役大學牲&#xff0c;半退休robomaster視覺隊員 寫在前面 本文章主要介紹的是如何通過開源的serial庫和虛擬串口實現上位機和下位機通信。 需求 假設下位機有這樣一個數據報發送給上位機 struct DataRecv {char start s;TeamColor color TeamColor::Blu…

Premiere Pro 2024 新功能有哪些?視頻剪輯軟件PR2024更新內容及問題修復

PR軟件“基于文本的編輯”中的填充詞檢測與批量刪除功能 “基于文本的編輯”可讓您檢測“呃”和“嗯”填充詞并批量刪除它們&#xff0c;從而使您的轉錄文本更加準確。就像處理停頓一樣&#xff0c;您可以單擊填充詞并將其從序列轉錄文本中刪除。填充詞與語言無關&#xff0c;…

STM32CubeIDE(CUBE-MX hal庫)----RTC時鐘,時鐘實時顯示

系列文章目錄 STM32CubeIDE(CUBE-MX hal庫)----初嘗點亮小燈 STM32CubeIDE(CUBE-MX hal庫)----按鍵控制 STM32CubeIDE(CUBE-MX hal庫)----串口通信 STM32CubeIDE(CUBE-MX hal庫)----定時器 STM32CubeIDE(CUBE-MX hal庫)----藍牙模塊HC-05&#xff08;詳細配置&#xff09; 前言…

【無標題】安裝環境

這里寫目錄標題 清華鏡像加速 安裝cuda11.3 PyTorch 1.10.1https://pytorch.org/get-started/previous-versions/[如果沒有可以點Previous pyTorch Versions&#xff0c;這里面有更多的更早的版本](https://pytorch.org/get-started/locally/) 復制非空文件夾cp: -r not specif…

傳染病傳播速度

題干 R0值是基本傳染數的簡稱&#xff0c;指的是在沒有采取任何干預措施的情況下&#xff0c;平均每位感染者在傳染期內使易感者個體致病的數量。數字越大說明傳播能力越強&#xff0c;控制難度越大。一個人傳染的人的數量可以用冪運算來計算。假設奧密克戎的R0為10&#xff0…

《LeetCode力扣練習》代碼隨想錄——字符串(翻轉字符串里的單詞---Java)

《LeetCode力扣練習》代碼隨想錄——字符串&#xff08;翻轉字符串里的單詞—Java&#xff09; 刷題思路來源于 代碼隨想錄 151. 反轉字符串中的單詞 雙指針 class Solution {public String reverseWords(String s) {char[] results.toCharArray();resultremoveSpace(result);r…

2023-12-05 Qt學習總結2

點擊 <C 語言編程核心突破> 快速C語言入門 Qt學習總結 前言五 Hello Qt!六 Qt控件和事件七 Qt信號和槽八 Qt自定義信號和槽總結 前言 要解決問題: 學習qt最核心知識, 多一個都不學. 五 Hello Qt! 現在我們已經有了一個空窗口工程, 傳統上, 我們要實現一個"Hello …

(三潮來襲)探尋2023年科技變革潮流與2024年前瞻展望

2023年對于IT行業來說是一個動蕩而又充滿變革的一年。隨著世界逐漸走出前幾年的挑戰&#xff0c;企業逐漸復蘇&#xff0c;但這個行業仍然在經歷著激烈的變革。在這個時候&#xff0c;我們看到了一些引人注目的技術變化和未來的趨勢。 一、2023年回顧 關鍵詞&#xff1a;Chat…

構建Servlet項目流程

第一步&#xff1a;創建maven項目 部分基礎 依賴的模板基礎部分如下 maven-archetype-quickstart: 這是最基本的Archetype&#xff0c;它創建一個包含簡單Java類和單元測試的項目。 maven-archetype-webapp: 這個Archetype創建一個簡單的Java web應用&#xff0c;包括一個serv…

【C++】算法庫(批量操作、搜索操作)

C算法庫 文章目錄 C算法庫批量操作for_eachfor_each_n 搜索操作all_of ,any_of ,none_offind, find_if, find_if_notfind_endstd::find_first_ofadjacent_findcount, count_ifequalsearchsearch_n 算法庫提供大量用途的函數&#xff08;例如查找、排序、計數、操作&#xff09;…

微信小程序 - PC端選擇ZIP文件

微信小程序 - PC端選擇文件 分享代碼片段場景分析解決思路附魔腳本chooseMediaZip 選擇附魔后的ZIP文件相關方法測試方法 參考資料 分享代碼片段 不想聽廢話的&#xff0c;直接看代碼。 https://developers.weixin.qq.com/s/UL9aojmn7iNU 場景分析 如果你的微信小程序需要選…

TypeScript入門實戰筆記 -- 開篇 為什么要選擇 TypeScript ?

typescript 在線編輯器http://typescript.p2hp.com/play?#code/JYOwLgpgTgZghgYwgAgJIFUDO1Uhge2QG8AoZc5YAEwC5kQBXAWwCNoBuMikOJiOzGCigA5pwrI4ANzhg4UAPwChozgF8SmmAxAIwwfCGRYcefAAoADlHyXMdDNii4CASmJdyCQ5nwAbCAA6P3wRKxs7ABpkAHJrW0wY1xINEhNnM3MiSlpkAEZonj46GIBrROQ1…

openEuler學習05-kernel升級

周末沒事&#xff0c;嘗試下openEuler的kernel升級 [rootlocalhost ~]# more /etc/os-release NAME"openEuler" VERSION"20.03 (LTS-SP3)" ID"openEuler" VERSION_ID"20.03" PRETTY_NAME"openEuler 20.03 (LTS-SP3)" ANSI_…

營收增速持續放緩,博通CEO期待AI崛起救場 | 百能云芯

博通作為蘋果等大型科技公司的芯片供應商&#xff0c;于周四發布了財報。盡管截至10月29日的第四季度營收增長了4%至93億美元&#xff0c;符合市場預期&#xff0c;但增速已經降至2020年以來的最低水平。 由于企業客戶和電信供應商在控制支出方面的謹慎態度&#xff0c;博通的銷…

IDEA構建springBoot新項目時JDK只有17和21,無法選擇JDK8解決方案

今天創建springboot新項目時&#xff0c;發現IDEA里JDK選項只有17和21&#xff0c;無法選擇本機的JDK8&#xff0c;網上查資料后發現是springboot2.7于11.24號后停止維護&#xff0c;基于2.7和java8的spring Initializ官方不再維護&#xff0c;解決方案是在server URL欄&#x…