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();
	}
}
相关推荐
adfreedom12 分钟前
如何避免每个业务系统中都有几十个枚举查询
后端
m0_6399069421 分钟前
SpringBoot自动配置原理
java·spring boot·后端
海鸥-w1 小时前
数据库冗余字段设计笔记
java·数据库·笔记·oracle
工具派1 小时前
PDF签名贴上去为什么发虚?尺寸、DPI与坐标的边界实测
java·python·pdf
萧瑟余晖1 小时前
Spring Data综合实战与调优详解
java·后端·spring
yuniko-n2 小时前
【JUC】原子类 LongAdder
java
瑞got2 小时前
java使用CountDownLatch做并行查询
java·开发语言
用户574385305112 小时前
第 2 章:裸写 Query(原生驱动)
后端
用户574385305112 小时前
第 8 章:Service 层的诞生(硬编码版)
后端