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();
	}
}
相关推荐
HarmonSir8 小时前
Octop 架构解析(外番篇):两种多 Agent 协作,Octop 群聊房间 vs dsh 工单授权
后端·架构
tong_li8 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(七):给工具循环上保险
后端·langchain
AI人工智能+电脑小能手9 小时前
集合按大小切片分批,这个Java工具类5个方法含基本类型数组
java·工具类·批量处理·集合分片·分批调用
bai2739 小时前
13 个文件名里 4 个打不开:Node 能建、cmd 和 PowerShell 却读不了
javascript·后端
用户298698530149 小时前
使用 Python 提取 Word 文档中的文本与图片
后端·python·api
AAA修煤气灶刘哥9 小时前
明明输入了完整报错,知识库怎么净给我相似答案?
人工智能·后端·算法
SimonKing9 小时前
高星红果短剧的TV版|桌面版都在这里了
java·后端·程序员
用户69371750013849 小时前
AI 用久了,我发现自己越来越不想找人聊天了
前端·后端·github
Rain的Java大神之路9 小时前
大四 Java 校招速成路线:从高频八股到可部署项目,30 天、8 周、12 周都能照着练
java·后端·面试
codigger9 小时前
订单超时自动关闭,7 种方案到底怎么选?
java·后端·程序员·编程