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();
	}
}
相关推荐
香菜TTT12 分钟前
Redis的哨兵机制
java·数据库·redis
withoutfear27 分钟前
IntelliJ IDEA卡顿内存分配不足和索引被频繁触发问题解决办法
java·ide·intellij-idea
大圣编蚕29 分钟前
Java StringWriter 详解:从入门到实战
java·开发语言·python
IT_陈寒32 分钟前
Vue的computed属性竟然坑了我一把
前端·人工智能·后端
行百里er38 分钟前
Spring Insight 里收到的 Span 是怎么存下来的
spring boot·spring·监控
老苗项目实战1 小时前
Java工程师高频面试题(基于近一年的真实面试记录整理)
java·开发语言·面试·预见猿份·老苗项目实战
薛定谔的悦1 小时前
一套储能 BMS 的充电电流限值,是怎么一步步算出来的
后端
陈皮波比茶1 小时前
分布式任务调度xxl-job
java·分布式
逐米时代2 小时前
AR加知识库提升一次修复率
后端·restful
王码码20352 小时前
Go语言CGO:Go与C交互
后端·golang·go·接口