Spring 6 第6章——单元测试:Junit
一、整合JUnit5
在之前的测试方法中,几乎都能看到以下两行代码:
ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
Xxxx xxx = context.getBean(Xxxx.class);
这两行代码的作用是创建Spring容器,最终获取到对象,但是每次测试都需要重复编写
针对上述问题,我们需要的是程序能自动帮我们创建容器,我们都知道JUnit无法知晓我们是否使用了Spring框架,更不用说帮我们创建Spring容器了
Spring提供了一个运行器,可以读取配置文件(或注解)来创建容器。我们只需要告诉它配置文件位置就可以了
这样一来,我们通过Spring整合JUnit可以使程序创建Spring容器了
(1)搭建子模块
- 搭建子模块spring6-junit
- 如图所示:
(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>
(3)添加配置文件
- 添加Spring的配置文件:
- 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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:component-scan base-package="com.atguigu.spring6"></context:component-scan> </beans>
(4)添加Java类
- 编写一个User类:
package com.atguigu.spring6.junit.junit5; import org.springframework.stereotype.Component; @Component public class User { public void run(){ System.out.println("user......"); } }
(5)测试
- 测试类和测试方法:
package com.atguigu.spring6.junit.junit5; 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 SpringTestJUnit5 { @Autowired private User user; //测试方法: @Test public void testUser(){ System.out.println(user); user.run(); } }
- 测试效果:
- 或者还有一种注解方式,也能成功
package com.atguigu.spring6.junit.junit5; 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 SpringTestJUnit5 { @Autowired private User user; //测试方法: @Test public void testUser(){ System.out.println(user); user.run(); } }
二、整合JUnit4
(1)添加依赖
- 引入相关依赖:
<!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
- 这时JUnit4和JUnit5的不同之处:
- 第二个不同之处:JUnit4JUnit5
(2)测试
- 测试代码:
package com.atguigu.spring6.junit.junit4; import com.atguigu.spring6.junit.junit5.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 SpringTestJUnit4 { @Autowired private User user; @Test public void testUser(){ System.out.println(user); user.run(); } }
- 测试效果:
原文地址:https://blog.csdn.net/weixin_74019079/article/details/145286795
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!