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();
	}
}
相关推荐
原野池予9 小时前
深入Java集合框架:HashMap源码解析(JDK 8)
java·开发语言
大黄评测9 小时前
Angular 表单:响应式表单高级用法,Typed Forms 类型化表单实战
后端
大勇前进9 小时前
Angular 变更检测深度讲解:OnPush 策略什么时候用、踩过哪些坑
后端
古法安卓9 小时前
Android-Direct Boot 阶段与 getFilesDir() 路径变更问题深度分析
android·java·android studio
拾光师9 小时前
Python 解析 JSON 日志:从一行数据到一份报告
后端
-今昭-9 小时前
Logstash 管理
java·服务器·前端
测试运维日常笔记9 小时前
VMware vSphere、ESXi 与 vCenter Server:核心概念与关系详解
java·开发语言
小强19889 小时前
RxJS 在 Angular 项目最佳实践:彻底告别内存泄漏,用好 asyncPipe
后端
RuoyiOffice9 小时前
SpringBoot3+Vue3 接口访问日志实战:@ApiAccessLog 慢接口、参数脱敏与 OperateLog 如何分工
spring boot·vue3·traceid·spring boot 3·访问日志·apiaccesslog·操作日志
风流 少年9 小时前
Spring AI 2.0:Flux
java·人工智能·spring