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();
	}
}
相关推荐
apcipot_rain1 小时前
C++转Java日记(06)
java·开发语言
脚本自动化开发4 小时前
柔光屏平板怎么选?华为 MatePad Air Z 柔光版学
后端
程序猿DD5 小时前
OctaFuse 2.19.0:重做管理后台界面与交互,模型目录同步更新
后端
金銀銅鐵6 小时前
带您看懂字节码指令(一):一帧里有什么
java·后端·ai编程
码事漫谈7 小时前
Agent 测评是什么?大白话讲一讲
后端
皮皮林5517 小时前
在 IDEA 里斗个地主不过分吧!
后端
nice_lcj5208 小时前
【从零理解 Spring AI · 14】RAG 的四个环节:加载、切分、向量化、检索
java·spring boot·embedding·向量数据库·rag·spring ai·ai应用开发
SomeOtherTime8 小时前
Rust所有权(AI回答)
开发语言·后端·rust
程序员cxuan9 小时前
离谱,AI 大佬顺手搓了个 YouTube ,而我顺手给他搓了个中文插件
人工智能·后端·程序员
学长毕设9 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端