Spring6:6 单元测试-JUnit

devtools/2025/4/1 2:48:33/

6、单元测试:JUnit

在之前的测试方法中,几乎都能看到以下的两行代码:

javascript">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、引入依赖

javascript"><dependencies><!--spring context依赖--><!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.2</version></dependency><!--springjunit的支持相关依赖--><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

javascript"><?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

java_70">6.1.4、添加java

javascript">package com.atguigu.spring6.bean;import org.springframework.stereotype.Component;@Component
public class User {public User() {System.out.println("run user");}
}

6.1.5、测试

javascript">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、添加依赖

javascript"><!-- junit测试 -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>

6.2.2、测试

javascript">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.ppmy.cn/devtools/171424.html

相关文章

本地基于Ollama部署的DeepSeek详细接口文档说明

前文&#xff0c;我们已经在本地基于Ollama部署好了DeepSeek大模型&#xff0c;并且已经告知过如何查看本地的API。为了避免网络安全问题&#xff0c;我们希望已经在本地调优的模型&#xff0c;能够嵌入到在本地的其他应用程序中&#xff0c;发挥本地DeepSeek的作用。因此需要知…

更改 vscode ! + table 默认生成的 html 初始化模板

vscode ! 快速成的 html 代码默认为&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>D…

STM32八股【3】------RAM和片上FLASH

1、RAM和FLASH构成 1.RAM ┌──────────────────────────┐ │ 栈区 (Stack) │ ← 从RAM顶端向下扩展&#xff08;存储局部变量、函数调用信息&#xff09; │--------------------------│ │ 堆区 (Heap) │ ← …

Java 基于微信小程序的美食推荐系统(附源码,文档)

博主介绍&#xff1a;✌Java徐师兄、7年大厂程序员经历。全网粉丝13w、csdn博客专家、掘金/华为云等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&#x1f3fb; 不…

Node.js 包管理配置文件详解:package.json、npmrc、package-lock.json 全面解析

目录 Node.js 包管理配置文件详解 1. package.json&#xff1a;Node.js 项目的核心配置文件 示例&#xff1a;完整的 package.json 配置 关键字段解析 2. package-lock.json&#xff1a;锁定依赖版本 示例&#xff1a;部分 package-lock.json 作用 如果 package-lock.j…

JavaScript 在 Chrome 中运行详解

JavaScript 在 Chrome 中运行详解 引言 JavaScript 是一种广泛使用的编程语言,常用于网页开发中实现动态效果和交互功能。Chrome 浏览器作为全球最受欢迎的浏览器之一,对 JavaScript 的支持非常出色。本文将详细介绍 JavaScript 在 Chrome 中运行的原理、环境以及一些优化技…

Oracle无法正常OPEN(一)

作为DBA在启动数据库时&#xff0c;可能会经常遇到数据库无法正常open的现象&#xff0c;其中原因有很多&#xff0c;今天我们讨论控制文件过旧的场景。即ORA-01207: file is more recent than control file - old control file 一、创建测试数据 SQL> create tablespace t…

spring boot jwt生成token

1、引入jwt依赖 <!--jwt的依赖--> <dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.18.3</version> </dependency> 2、创建TokenUtils工具类 package com.pn.utils;import com.…