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();
	}
}
相关推荐
ekuoleung10 分钟前
量化平台中的 DSL 设计与实现:从规则树到可执行策略
前端·后端
就像风一样抓不住12 分钟前
Java 手机号校验工具类
java
小研说技术17 分钟前
实时通信对比,一场MCP协议的技术革命
前端·后端·面试
凤山老林20 分钟前
26-Java this 关键字
java·开发语言
ServBay21 分钟前
2026年 Go 开发中没有它就不行的 10 个库
后端·go
焦糖玛奇朵婷44 分钟前
解锁扭蛋机小程序的五大优势
java·大数据·服务器·前端·小程序
SamDeepThinking1 小时前
别让一个超时的第三方http接口拖垮所有接口
java·后端·架构
YaBingSec1 小时前
玄机靶场:供应链安全-供应链应急-Part2 通关笔记
java·笔记·安全
Gerardisite1 小时前
企微机器人开发指南
java·python·机器人·自动化·企业微信
我母鸡啊1 小时前
软考架构师故事系列-操作系统
后端