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();
	}
}
相关推荐
二哈赛车手6 分钟前
新人笔记---Spring AI的Advisor以及其底层机制讲解(涉及源码),包含一些遇见的Spring AI的Advisor缺陷问题的解决方案
java·人工智能·spring boot·笔记·spring
AC赳赳老秦10 分钟前
接口测试自动化:用 OpenClaw 对接 Postman,实现批量回归测试、测试报告自动生成与推送
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
两年半的个人练习生^_^11 分钟前
Java日志框架和使用、日志记录规范
java·开发语言·开发规范
浮游本尊13 分钟前
服务请求附件:从上传到预览、下载的实现详解
后端
程序员辉哥20 分钟前
从零构建Agent智能体系列01-从零理解智能体
后端·openai·ai编程
客场消音器42 分钟前
我用两周半 Vibe Coding 做了一个前额叶训练的微信小程序
前端·javascript·后端
pq2171 小时前
最简单的理解synchronized锁升级
java
杨凯凡1 小时前
【032】排查入门:jstack、heap dump、Arthas 初识
java·开发语言·后端
pq2171 小时前
Spring FactoryBean源码解析
java·spring boot·spring
其实防守也摸鱼1 小时前
无线网络安全--实验 规避WLAN验证之发现隐藏的SSID
java·开发语言·网络·安全·web安全·智能路由器·无线网络安全