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();
	}
}
相关推荐
xiaobaoyu10 分钟前
ssm
后端
Nick_zcy12 分钟前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
源码宝14 分钟前
基于 SpringBoot + Vue 的医院随访系统:技术架构与功能实现
java·vue.js·spring boot·架构·源码·随访系统·随访管理
王中阳Go23 分钟前
2026年了,还在纠结后端转AI要不要死磕Python?试试Go吧
后端·go·ai编程
用户83562907805134 分钟前
用 Python 轻松在 Excel 工作表中应用条件格式
后端·python
red1giant_star38 分钟前
Python根据文件后缀统计文件大小、找出文件位置(仿Everything)
后端·python
长大19881 小时前
每秒10万写入的订单系统:MySQL分库分表、缓冲设计、批量写入优化实战
后端
渐儿1 小时前
缓存一致性与分布式锁:工程踩坑全解
后端
长大19881 小时前
为什么我加了索引,查询反而更慢了?
后端
阿聪谈架构1 小时前
第08章:MCP 模型上下文协议(下)
人工智能·后端