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();
	}
}
相关推荐
掘金者阿豪4 分钟前
上线前能跑,上线后挂了,三个隐性的SQL陷阱
后端
用户83562907805116 分钟前
如何使用 Python 为 PDF 添加和管理图层
后端·python
名侦探72224 分钟前
构建 Agent 的五大难点与解决方案
java·大数据·人工智能
Demons_kirit27 分钟前
图库原理(以阿里云OSS为案例)
java·阿里云·云计算
SimonKing30 分钟前
那个号称"对标 Spring"的国产框架 Solon,到底行不行?
java·后端·程序员
Lethehong31 分钟前
飞算Java:从需求梳理到风险闭环,搭建云盾企业信息安全与权限管理平台
java·开发语言
霸道流氓气质35 分钟前
SpringBoot中使用字典驱动的动态路由示例
java·spring boot·后端
万亿少女的梦16844 分钟前
基于Spring Boot、Vue与MySQL的高校实习信息管理系统设计与实现
spring boot·mysql·vue·权限管理·restful api
学计算机的计算基1 小时前
操作系统八股文:进程与线程全面梳理(附调度算法+IPC+锁机制)
java·算法
大黄说说1 小时前
Hyperf 协程 PHP 实战:高并发秒杀接口从 0 到 1 落地
junit