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();
	}
}
相关推荐
W_LuYi1858 分钟前
手撸极简zkEVM验证器:RISC-V电路实践
java·risc-v
BingoGo9 分钟前
PHP 在领域驱动(DDD)设计中的核心实践
后端·php
AI人工智能+电脑小能手17 分钟前
【大白话说Java面试题 第102题】【并发篇】第2题:volatile 能否保证线程安全?
java·安全·面试
KobeSacre23 分钟前
JUC 概述
java·开发语言
小bo波1 小时前
形式化方法 × UML
java·软件工程·uml·面向对象·形式化方法·tla+
掘金者阿豪1 小时前
终于!我的第二本书正式出版,吃透 Agentic AI 核心不踩坑
javascript·后端
就叫_这个吧1 小时前
IDEA中Javaweb项目创建+servlet,实现简单的信息录入获取
java·servlet·intellij-idea·web
二月龙1 小时前
Redis 缓存设计避坑指南:穿透、击穿、雪崩与一致性问题
后端
程序员Jelena1 小时前
接口调用的代码实现:从入门到实战
java
掘金者阿豪1 小时前
运营不会SQL怎么办?我把数据库变成了大家都会用的表格
后端