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();
	}
}
相关推荐
一只大袋鼠3 分钟前
SpringBoot 入门学习笔记(二)Web 开发基础
spring boot·笔记·学习
阿维的博客日记5 分钟前
简单介绍一下CompletableFuture,从最简单的用法介绍,
java
zandy10118 分钟前
2026 BI平台安全治理体系构建:从权限模型到零信任架构
java·开发语言
SuniaWang10 分钟前
《Agentx专栏》02-技术选型:预算有限时如何做出正确的技术决策
java·spring·架构·langchain·milvus·agenx·opl
羡寒.10 分钟前
接口突然变慢,你怎么排查?
java·后端·spring
ltlovezh11 分钟前
AAC 元数据:ADTS 与 ASC 的区别、转换和常见坑
后端·ffmpeg·音视频开发
zuowei288918 分钟前
编程语言对比:C/C++/Java/C#/PHP
java·c语言·c++
百数平台20 分钟前
功能更新——百数详情页“数据简报”与“关联标签页”配置指南
java·服务器·前端
接着奏乐接着舞29 分钟前
java lambda表达式
java·开发语言·python
金銀銅鐵30 分钟前
[Java] 自己写程序,来解析字段的 descriptor
java·后端