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();
	}
}
相关推荐
程序员梅雨18 分钟前
Linux & Shell 实用干货
linux·运维·服务器·后端·面试·php
lemon_sjdk19 分钟前
javafx-Image图像(2)
java·javafx
大牧师25 分钟前
TypeORM 入门教程
后端·sql·mysql·orm·nest·typeorm
IT_陈寒26 分钟前
Vite静态资源路径这个大坑害我调了一下午
前端·人工智能·后端
武哥聊编程27 分钟前
【AI实战项目】基于SpringAI+Springboot+Vue的AI面试刷题训练平台
vue.js·人工智能·spring boot·springai
oliver_sys_log35 分钟前
绕过 Yearning 查询体验限制:我做了一个 DataGrip 只读 SQL 代理
后端·mysql
小七在进步36 分钟前
C++入门(2)
java·jvm·c++
水深火乐37 分钟前
golang-jwt v5 入门
后端
carson95539 分钟前
基于springboot和vue的文本文件上传下载在线编辑功能
后端
To0R40 分钟前
Agent 上线前必须过的五道门(附 Spring Boot 实现)
java