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();
	}
}
相关推荐
计算机毕设VX:Fegn089537 分钟前
计算机毕业设计|基于springboot + vue在线考试系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
我居然是兔子3 小时前
异常练习:在试错中吃透Java异常处理的底层逻辑
java·开发语言
Tony Bai4 小时前
Go 的 AI 时代宣言:我们如何用“老”原则,解决“新”问题?
开发语言·人工智能·后端·golang
CC.GG4 小时前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
Overt0p5 小时前
抽奖系统(4)
java·spring boot·tomcat
用户47949283569155 小时前
性能提升 40 倍!实战 PostgreSQL FDW 解决微服务跨库查询难题
数据库·后端
计算机毕设VX:Fegn08955 小时前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
想做后端的小C6 小时前
Java:接口回调
java·开发语言·接口回调
爱学习的小可爱卢6 小时前
JavaEE进阶——Spring核心设计模式深度剖析
java·spring·设计模式
Victor3566 小时前
Hibernate(9)什么是Hibernate的Transaction?
后端