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();
	}
}
相关推荐
澈20721 分钟前
C++并查集:高效解决连通性问题
java·c++·算法
易安说AI21 分钟前
Codex 直接住进 JetBrains IDE 里:AI Agent 正在接管熟悉的开发入口
后端
子兮曰1 小时前
Node.js v26.1.0 深度解读:FFI、后量子密码与调试器的进化
前端·后端·node.js
2401_873479402 小时前
运营活动被薅羊毛怎么防?用IP查询+设备指纹联动封堵漏洞
java·网络·tcp/ip·github
ShiJiuD6668889992 小时前
大事件板块一
java
摇滚侠2 小时前
@Autowired 和 @Resource 的区别
java·开发语言
Wy_编程2 小时前
go语言中的结构体
开发语言·后端·golang
SeaTunnel2 小时前
(八)收官篇 | 数据平台最后一公里:数据集成开发设计与上线治理实战
java·大数据·开发语言·白鲸开源
Ting-yu3 小时前
SpringCloud快速入门(7)---- 数据隔离
spring boot·spring·spring cloud
吴声子夜歌3 小时前
Java——线程的基本协作机制
java·线程协作