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();
	}
}
相关推荐
葡萄城技术团队14 分钟前
InfluxDB 2\.x 深度解析:核心架构、Flux 函数与制造业落地指南(三)
java·开发语言·架构
Super 含16 分钟前
Android 启动优化(五):线程、GC 与 IO 为什么会拖慢启动?
java·服务器·数据库
counting money17 分钟前
Java IO流详解:从InputStream到文件操作实战
java·开发语言·python
程序员小八7771 小时前
上海百度B端java后端日常实习一面
java·开发语言
北斗落凡尘1 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
demo007x2 小时前
Hermes-Agent 技术架构
前端·后端·agent
mldong2 小时前
零依赖的秘密:8 大 SPI 设计
java·架构
__zRainy__4 小时前
Node系列 · Node基础:net 模块
后端·node.js