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();
	}
}
相关推荐
Nebula_g11 分钟前
JavaSE加强:Stream流
java·开发语言·windows·stream·javase·流
估值探索者13 分钟前
【Python量化系统工程实战 #08】从脚本到生产:量化系统上线 checklist 的最小闭环
java·开发语言·jvm·python·数据挖掘·数据·股票数据api接口
Wang's Blog16 分钟前
Java框架 SpringCloud 快速入门: Nacos 环境隔离
java·开发语言·spring cloud
泡泡oO21 分钟前
“如果你还在用Superpowers,那我不要和你说话”
前端·后端·全栈
万年咸鱼27 分钟前
解决C语言的内存释放机制
java·c语言·python
一木 之林44 分钟前
DeepSeek Agent 开发(三)
java·大数据·linux
武子康1 小时前
LingBot-Video 怎么选推理路径?8 步 DMD 不等于小显存
人工智能·后端
打工仔折腾 AI1 小时前
Docker镜像分层与卷挂载到底怎么工作:一次文件系统层面的实测分析
运维·人工智能·后端·python·docker·容器·性能优化
denlaku1 小时前
JDK 10 新特性详解
java·开发语言
程序员耄耋1 小时前
JDK21的下载与安装(2025.8.2)
java