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();
	}
}
相关推荐
逍遥德5 分钟前
PostgreSQL ---【序列】用法详解
数据库·后端·sql·postgresql
逍遥德6 分钟前
PostgreSQL --- 自增主键【序列】的避坑指南
数据库·后端·sql·mysql·postgresql·sqlserver
段ヤシ.12 分钟前
【Java框架】知识点汇总Day7:Spring Boot +Vue(持续更新)
vue.js·spring boot·后端·框架
土狗TuGou13 分钟前
SQL进阶笔记 · 第1篇:存储引擎
java·数据库·笔记·后端·sql·mysql
雨落在了我的手上19 分钟前
Java数据结构(一):初识集合框架
java·开发语言
码语智行21 分钟前
Spring Security自定义AuthenticationManager实现手机号/密码双认证
java·后端·spring
SuniaWang21 分钟前
《AgentX 专栏》07-全链路可观测:用OpenTelemetry+Jaeger让每次AI对话都可追踪可复盘
java·人工智能·spring·架构·langchain·opentelemetry·agenx
fengxin_rou21 分钟前
【从零开始的JUC并发第五章】:线程池详解
java·jvm·spring
咖啡八杯26 分钟前
GoF设计模式——装饰模式
java·算法·设计模式·装饰器模式
_Aaron___26 分钟前
RAG 知识库越用越脏?先把“增量更新”设计清楚
java·人工智能