Spring6:6 单元测试-JUnit

embedded/2025/4/1 8:00:56/

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/embedded/176853.html

相关文章

爬虫:Requests-HTML的详细使用

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 1. Requests-HTML概述1.1 为何选择Requests-HTML?1.2 什么是Requests-HTML?1.3 主要特性1.4 安装 `requests-html`2. 基本用法2.1 发送 HTTP 请求2.2 解析 HTML2.2.1 使用 CSS 选择器2.2.2 使用 XPath2.2.3 使用 `sea…

23种设计模式中的责任链模式

允许多个对象都有机会处理请求&#xff0c;从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链&#xff0c;并沿着这条链传递请求&#xff0c;直到有一个处理器处理该请求为止。 责任链模式是一种处理请求的模式。它让多个处理器都有机会处理请求&#xff0c;直…

Linux之基本命令和格式

先给大家介绍几个学习过程中会运用到的软件 VMMare&#xff1a;虚拟机软件作用是用来安装其他具体操作系统的平台 Redhat&#xff1a;Linux操作系统&#xff0c;用来操作软件和命令的系统平台&#xff0c;可以建多个 Xshell&#xff0c;Xftp&#xff1a;辅助工具&#xff0c…

JVM 内存参数调优详解

1. ​堆内存控制 -Xms4g # 初始堆内存大小&#xff08;JVM 启动时分配的堆内存&#xff09; -Xmx4g # 最大堆内存大小&#xff08;JVM 堆内存的上限&#xff09; ​详细说明 ​作用&#xff1a; -Xms&#xff08;Initial Heap Size&#…

APL语言的压力测试

APL语言的压力测试&#xff1a;探索极限与性能挑战 引言 APL&#xff08;A Programming Language&#xff09;是一种用于数组处理的编程语言&#xff0c;它以其简洁的语法和强大的数组操作能力广受欢迎。由于其独特的表达方式&#xff0c;APL在金融、工程和科学计算等领域被广…

贪心算法 力扣hot100热门面试算法题 面试基础 核心思路 背题

贪心算法 买卖股票的最佳时机 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/ 核心思路 如果假设今日买入&#xff0c;未来最高点是未知的&#xff0c;需要遍历后续数组&#xff0c;所以时间复杂度变成n^2&#xff1b; 那么如果假设今日卖出&#xff0c;遍…

诡异的服务重启原因探索

背景 同事做了一个订单导出功能,大约15万的数据量,使用golang语言开发,使用了ShardingSphere分表组件,通过多协程并行把15万的数据一次性读到内存,写excel并上传阿里OSS。 在本地测试没有发生什么异常的情况。部署到服务器,执行导出功能程序就会自动重启。 探索过程 …

Kafka是如何实现幂等性的??

Kafka通过幂等生产者&#xff08;Idempotent Producer&#xff09;机制来实现消息的幂等性&#xff0c;确保每条消息在Kafka中只被处理一次&#xff0c;即使在生产者重试发送的情况下也不会导致重复消息。以下是Kafka实现幂等性的详细说明&#xff1a; 1. 幂等生产者的基本概念…