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();
	}
}
相关推荐
Highcharts.js6 小时前
成分占比饼图代码|常用可视化图表开发示例
java·开发语言·仪表盘·highcharts·实战代码·响应式图表
码事漫谈8 小时前
WorkBuddy 固然很香,但豆姐她免费呀
后端
西柚小萌新8 小时前
【人工智能:LangGraph】--3.控制流(Edge)
java·服务器·数据库
金銀銅鐵8 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
zark_chuan8 小时前
怎么让 AI 写十万行代码不腐化:我的 AGENTS.md、Pre-Push 门禁与活体 Wiki
后端
r_oo_ki_e_10 小时前
牛牛与切割机
java·算法
Rain的Java大神之路10 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
泡海椒11 小时前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
wuminyu11 小时前
OpenJDK学习笔记:Native事件队列与Java回调背压
java·linux·c语言·jvm·c++
源码_空间11 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端