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();
	}
}
相关推荐
骇客野人几秒前
.gitignore文件常用设置
java
神奇的程序员3 分钟前
实现一个内网服务监测告警系统
后端·自动化运维
马卡巴卡4 分钟前
Spring监听器(ApplicationEvent):比MQ更轻的异步神器!
后端
QZQ541886 分钟前
go中单例模式以及使用反射破坏单例的方法
后端
bill4479 分钟前
BPMN2.0,flowable工作流,【用户任务】使用【任务监听器】动态设置下一步处理人
java·工作流引擎·flowable·bpmn
Cricyta Sevina9 分钟前
Java 语言多线程核心概念全解析
java·开发语言
bcbnb10 分钟前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
后端
Java水解14 分钟前
GO语言特性介绍,看这一篇就够了!
后端·go
shenzhenNBA18 分钟前
如何在python文件中使用日志功能?简单版本
java·前端·python·日志·log