Spring之单元测试
2024-01-07 22:32:18
Spring提供了一个运行器,可以读取配置文件(或注解)来创建容器。
1 整合Junit5
1.1 引入依赖
<dependencies>
<!--spring context依赖-->
<!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.1</version>
</dependency>
<!--spring对junit的支持相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.1.1</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>
1.2 配置扫描
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.giser.spring6" />
</beans>
1.3 基础实体
package com.giser.spring6.junit5;
import org.springframework.stereotype.Component;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:37:52
*/
@Component
public class User {
public User() {
System.out.println("run user");
}
}
1.4 测试
package com.giser.spring6.junit5;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:41:24
*/
// 方式二
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:spring-bean.xml")
// 方式一 推荐
@SpringJUnitConfig(locations = "classpath:spring-bean.xml")
public class SpringJunit5Test {
@Autowired
private User user;
@Test
public void testUser(){
System.out.println(user);
}
}
2 整合Junit4
2.1 引入依赖
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.2 实体
package com.giser.spring6.junit4;
import org.springframework.stereotype.Component;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:37:52
*/
@Component
public class Junit4User {
public Junit4User() {
System.out.println("run junit4User");
}
}
2.3 测试
package com.giser.spring6.junit4;
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;
/**
* @author giserDev
* @description
* @date 2024-01-07 19:41:24
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-bean.xml")
public class SpringJunit4Test {
@Autowired
private Junit4User junit4User;
@Test
public void testUser(){
System.out.println(junit4User);
}
}
文章来源:https://blog.csdn.net/SUNBOYmxbsH/article/details/135445851
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!