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();
	}
}
相关推荐
海里真的有鱼7 分钟前
Spring Boot 中整合 Kafka
后端
idealzouhu13 分钟前
Java 并发编程 —— AQS 抽象队列同步器
java·开发语言
布瑞泽的童话13 分钟前
无需切换平台?TuneFree如何搜罗所有你爱的音乐
前端·vue.js·后端·开源
听封17 分钟前
Thymeleaf 的创建
java·spring boot·spring·maven
写bug写bug23 分钟前
6 种服务限流的实现方式
java·后端·微服务
离开地球表面_9933 分钟前
索引失效?查询结果不正确?原来都是隐式转换惹的祸
数据库·后端·mysql
楠枬34 分钟前
双指针算法
java·算法·leetcode
Victor35636 分钟前
Oracle(138)如何监控数据库性能?
后端
奔驰的小野码40 分钟前
java通过org.eclipse.milo实现OPCUA客户端进行连接和订阅
java·开发语言