Spring Boot整合Junit,@RunWith和@SpringBootTest的使用

Spring Boot整合Junit,@RunWith和@SpringBootTest的使用

1、在pom.xml添加junit启动器

java 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
</dependency>

2、编写测试类

java 复制代码
/**
 *  main方法:
 *		ApplicationContext ac=new 
 *       			ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 *  junit与spring整合:
 *      @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *   	@Contextconfiguartion("classpath:applicationContext.xml")  
 */
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes={App.class})//App是项目的启动类名称
public class UserServiceTest {

	@Autowired
	private UserServiceImpl userServiceImpl;
	
	@Test
	public void testAddUser(){
		this.userServiceImpl.addUser();
	}
}
相关推荐
摇滚侠17 分钟前
Spring Boot 3零基础教程,新特性 ProblemDetails,笔记50
spring boot·笔记
青云交20 分钟前
Java 大视界 -- Java 大数据在智能农业温室环境调控与作物生长模型构建中的应用
java·机器学习·传感器技术·数据处理·作物生长模型·智能农业·温室环境调控
曾经的三心草27 分钟前
SpringAI5-智能聊天机器⼈
java·springai
微露清风29 分钟前
系统性学习C++-第八讲-vector类
java·c++·学习
安冬的码畜日常35 分钟前
【JUnit实战3_12】第七章:用 Stub 模拟进行粗粒度测试
测试工具·junit·单元测试·junit5·stub模拟·模拟技术·stub 桩
roykingw1 小时前
【思想比实现更重要】高并发场景下如何保证接口幂等性
java·web安全·面试
紫荆鱼2 小时前
设计模式-命令模式(Command)
c++·后端·设计模式·命令模式
864记忆2 小时前
Spring Boot 项目标准目录结构
java
编码追梦人2 小时前
深耕 Rust:核心技术解析、生态实践与高性能开发指南
开发语言·后端·rust
2301_772093562 小时前
KVSTORE_Pain point_tuchuang_ROS2
java·开发语言·1024程序员节