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();
	}
}
相关推荐
你怎么知道我是队长1 天前
C语言---未定义行为
java·c语言·开发语言
没有bug.的程序员1 天前
Java 序列化:Serializable vs. Protobuf 的性能与兼容性深度对比
java·开发语言·后端·反射·序列化·serializable·protobuf
愚公移码1 天前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
Remember_9931 天前
【LeetCode精选算法】滑动窗口专题一
java·数据结构·算法·leetcode·哈希算法
开开心心就好1 天前
音频编辑工具,多端支持基础剪辑易操作
java·网络·windows·java-ee·电脑·maven·excel
凯子坚持 c1 天前
Protocol Buffers C++ 进阶数据类型与应用逻辑深度解析
java·服务器·c++
黎雁·泠崖1 天前
Java面向对象:对象内存图+成员与局部变量
java·开发语言
窗边鸟1 天前
小白日记之java方法(java复习)
java·学习
我爱娃哈哈1 天前
SpringBoot + Spring Security + RBAC:企业级权限模型设计与动态菜单渲染实战
spring boot·后端·spring
砚边数影1 天前
AI数学基础(一):线性代数核心,向量/矩阵运算的Java实现
java·数据库·人工智能·线性代数·矩阵·ai编程·金仓数据库