spring —— spring 整合 junit

如果不使用 spring 整合 junit,每次取对象时都要建立 spring 容器,然后从 spring 容器中提取对象,也就是以下两行代码:

//建立spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
//从spring容器中提取对象
XX xx = (XX) context.getBean("XX");

使用 spring 整合 junit,则可以省略这一步骤。根据 junit 版本不同,可分为 spring 整合 junit5,或者 spring 整合 junit4。

一、spring 整合 junit5

(一)pom 文件中需添加依赖: 

<dependency><!--spring整合junit依赖--><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.1.3</version>
</dependency>
<dependency><!--junit5依赖--><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.0</version>
</dependency>

(二)spring-config.xml 文件配置:

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

(三)java 代码:

 目标类:

package com.atguigu.spring6;import org.springframework.stereotype.Component;@Component
public class User {
}

测试类(第一种写法):

package com.test;import com.atguigu.spring6.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;@SpringJUnitConfig(locations = "classpath:bean.xml")
public class UserTest {@Autowiredprivate User user;@Testpublic void run(){System.out.println(user);}
}

测试类(第二种写法):

package com.test;import com.atguigu.spring6.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;@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean.xml")
public class UserTest {@Autowiredprivate User user;@Testpublic void run(){System.out.println(user);}
}

 二、spring 整合 junit4

(一)pom 文件中需添加依赖:

<dependency><!--spring整合junit依赖--><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.1.3</version>
</dependency>
<dependency><!--junit4依赖--><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version>
</dependency>

(二)spring-config.xml 文件配置: 

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

(三)java 代码:

 目标类:

package com.atguigu.spring6;import org.springframework.stereotype.Component;@Component
public class User {
}

测试类:

package com.test;import com.atguigu.spring6.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:bean.xml")
public class UserTest {@Autowiredprivate User user;@Testpublic void run(){System.out.println(user);}
}

注意:

 spring 整合 junit5 和 junit4,除了依赖不同,注解不同,@Test 也不相同。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/49818.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

探索 IPython %%sql 魔术:数据库交互的高效工具

探索 IPython %%sql 魔术&#xff1a;数据库交互的高效工具 在数据科学和分析领域&#xff0c;IPython 提供了一个强大的交互式环境&#xff0c;允许用户执行 Python 代码并与各种数据源进行交互。%%sql 魔术命令是 IPython 环境中的一个特殊命令&#xff0c;它允许用户直接在…

【时间动作定位】End-to-end Temporal Action Detection with Transformer 论文阅读

End-to-end Temporal Action Detection with Transformer 论文阅读 AbstractI. INTRODUCTIONII. RELATED WORKIII. TADTRA. ArchitectureB. Training and Inference IV. EXPERIMENTSV. CONCLUSION 文章信息&#xff1a; 发表于&#xff1a; IEEE Transactions on Image Proce…

四、GD32 MCU 常见外设介绍 (7) 7.I2C 模块介绍

7.1.I2C 基础知识 I2C(Inter-Integrated Circuit)总线是一种由Philips公司开发的两线式串行总线&#xff0c;用于内部IC控制的具有多端控制能力的双线双向串行数据总线系统&#xff0c;能够用于替代标准的并行总线&#xff0c;连接各种集成 电路和功能模块。I2C器件能够减少电…

【MQTT协议与IoT通信】MQTT协议的使用和管理

MQTT协议与IoT通信&#xff1a;MQTT协议的使用和管理 目录 引言MQTT协议概述 什么是MQTTMQTT的工作原理 MQTT协议的关键特性 轻量级与高效性发布/订阅模式质量服务等级(QoS)持久会话安全性 MQTT协议的使用方法 设置MQTT Broker连接MQTT Client发布消息订阅主题断开连接 MQTT协…

策略+工厂设计模式的应用

问题 比如有下面一段业务&#xff0c;一个自动售货机&#xff0c;根据用户选择不同的饮料&#xff0c;出对应的货 public class NoDesignDemo {public void ifElse(String parameter) {if ("Pepsi".equalsIgnoreCase(parameter)) {System.out.println("百事可…

[ptrade交易实战] 第十八篇 期货查询类函数和期货设置类函数

前言 今天主要和大家分享的是期货查询类的函数和期货设置类的函数&#xff01; 具体的开通渠道可以看文章末尾&#xff01; 一、get_margin_rate—— 获取用户设置的保证金比例 保证金是期货交易中的一个重点&#xff0c;这个函数就是用来获取我们设置的保证金比例的&#…

整合StarRocks主键表全部知识点

总结StarRocks主键表的全部内容的集合&#xff08;V3.2版本&#xff09; 一、基本功能 主键非空约束&#xff0c;任何一个字段都不可以为空支持导入数据时删除数据操作(CDC)支持部分更新支持独立的排序键&#xff08;只有主键表支持独立排序键&#xff0c;而且可以不为key键&…

极限科技闪耀 2024 可信数据库发展大会,多款自研产品引领搜索技术新纪元

北京&#xff0c;7 月 16、17 日 —— 在由中国信息通信研究院&#xff08;中国信通院&#xff09;与中国通信标准化协会联合主办&#xff0c;InfoQ 协办的“2024 可信数据库发展大会”&#xff08;TDBC 2024&#xff09;上&#xff0c;极限科技凭借其前沿的搜索技术创新与卓越…

【相机与图像】1. 相机模型的介绍:内参、外参、畸变参数

想着整理下相机模型&#xff08;内容上参考 slam十四讲&#xff09;、相机的内外参标定。方便自己的使用和回顾。 不过&#xff0c;内外参标定啥时候记录随缘 -_- 概述 【构建相机模型】 相机将三位世界中的坐标点&#xff08;单位为米&#xff09;映射到二维图像平面&#xff…

py Qt5学习记录

1.Qt5的安装 可参考一些博客如&#xff1a;Python 小白从零开始 PyQt5 项目实战&#xff08;2&#xff09;菜单和工具栏_pyqt 二级菜单-CSDN博客 2.Qt5的界面显示 3.新建一个工具栏并打开本地文件方法 &#xff08;1&#xff09;在Qt5界面右下角有个“动作编辑器”&#xff…

学习笔记5:缓存穿透

缓存穿透 缓存穿透是指在缓存系统中&#xff0c;当一个请求的查询结果为空时&#xff0c;这个请求会直接穿透缓存系统&#xff0c;访问后端的数据库。如果这种情况频繁发生&#xff0c;会对数据库造成较大的压力&#xff0c;甚至可能导致数据库崩溃。 在正常情况下&#xff0…

对pytorch optimizer中state_dict、state、param_groups的简要理解

先说结论&#xff1a; state_dict()&#xff1a;一个dict&#xff0c;里面有两个key&#xff08;state和param_groups&#xff09;&#xff0c; state这个key对应的value是各个权重对应的优化器状态。具体来说&#xff0c;一个model有很多权重&#xff0c;model.parameters()会…

MyBatis相关问题汇总

sql预编译原理 https://www.cnblogs.com/Createsequence/p/16963891.html MyBatis一级缓存&二级缓存 mybatis的缓存机制&#xff08;一级缓存二级缓存和刷新缓存&#xff09;和mybatis整合ehcache_mybatis缓存机制-CSDN博客

每日一题~961div2A+B+C(阅读题,思维,数学log)

A 题意&#xff1a;给你 n*n 的表格和k 个筹码。每个格子上至多放一个 问至少占据多少对角线。 显然&#xff0c;要先 格数的多的格子去放。 n n-1 n-2 …1 只有n 的是一个&#xff08;主对角线&#xff09;&#xff0c;其他的是两个。 #include <bits/stdc.h> using na…

基于Java和MySQL的数据库优化技术

基于Java和MySQL的数据库优化技术 大家好&#xff0c;我是微赚淘客系统3.0的小编&#xff0c;是个冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天我们将探讨如何基于Java和MySQL进行数据库优化&#xff0c;提升系统的性能和稳定性。我们将从查询优化、索引使用…

管理和迁移Conda环境两种方法:conda env export 和 Conda-Pack

在管理和迁移Conda环境时&#xff0c;通常有两种常用的方法&#xff1a;conda env export 和 Conda-Pack。这两种方法各有优缺点&#xff0c;根据具体需求可以选择合适的方法。 方法一&#xff1a;Conda env export conda env export 是Conda自带的命令&#xff0c;用于导出当…

基于微信小程序图书馆座位预约管理系统设计与实现

1.1选题动因 当前的网络技术&#xff0c;软件技术等都具备成熟的理论基础&#xff0c;市场上也出现各种技术开发的软件&#xff0c;这些软件都被用于各个领域&#xff0c;包括生活和工作的领域。随着电脑和笔记本的广泛运用&#xff0c;以及各种计算机硬件的完善和升级&#x…

JS 事件循环(Event Loop)机制

事件循环机制的作用 事件循环机制是 JS 的一种执行机制&#xff0c;一种可以实现异步编程的机制。 因为 JS 是单线程的&#xff0c;单线程意味着所有任务需要排队执行。但是有一些 API&#xff08;比如&#xff1a;定时器和 Ajax 等&#xff09;是需要等待一定的时间才能得到…

【Python】一文向您详细介绍 K-means 算法

【Python】一文向您详细介绍 K-means 算法 下滑即可查看博客内容 &#x1f308; 欢迎莅临我的个人主页 &#x1f448;这里是我静心耕耘深度学习领域、真诚分享知识与智慧的小天地&#xff01;&#x1f387; &#x1f393; 博主简介&#xff1a;985高校的普通本硕&#xff…

Visual Studio 2022新建 cmake 工程测试 tensorRT 自带样例 sampleOnnxMNIST

1. 新建 cmake 工程 vs2022_cmake_sampleOnnxMNIST_test( 如何新建 cmake 工程&#xff0c;请参考博客&#xff1a;Visual Studio 2022新建 cmake 工程测试 opencv helloworld ) 2. 删除默认生成的 vs2022_cmake_sampleOnnxMNIST_test.h 头文件 3. 修改默认生成的 vs2022_cma…