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();
	}
}
相关推荐
L-影1 小时前
HashMap详解-上篇
java·开发语言·spring·hashmap
爱笑的源码基地1 小时前
什么是智能预问诊系统?一套java版预问诊成品源码
java·源码·程序·预问诊·智能问诊·预问诊源码
她说..1 小时前
幂等问题分析
java·jvm·spring boot·spring·spring cloud·intellij-idea
摇滚侠2 小时前
《On Java 中文版 基础卷》阅读笔记 集合 24
java·开发语言·笔记
小小测试开发2 小时前
rsync 源路径少一个斜杠,--delete 删的就不是你以为的那一层
java·前端·数据库·人工智能
智购科技无人售货机工厂店2 小时前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
Nazarick3 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
黑色的白兔No14 小时前
若依平台cloud版本搭建
java·spring cloud·微服务·nacos·vue·若依
syf0611134 小时前
【Java】synchronized 关键字详解:从字节码到对象头与锁升级
java·开发语言
笑鸿的学习笔记5 小时前
C++笔记之信号量和引用计数的概念是否等价?
java·c++·笔记