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();
	}
}
相关推荐
巫山老妖1 小时前
从零开发一个掘金自动发布 Skill,并上架 Clawhub
后端
颜酱2 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
雨中飘荡的记忆2 小时前
零拷贝技术深度解析
后端
uzong3 小时前
十年老员工的项目管理实战心得:有道有术
后端
Victor3564 小时前
MongoDB(31)索引对查询性能有何影响?
后端
Victor3564 小时前
MongoDB(30)如何删除索引?
后端
lizhongxuan5 小时前
多 Agent 协同机制对比
后端
IT_陈寒5 小时前
SpringBoot项目启动慢?5个技巧让你的应用秒级响应!
前端·人工智能·后端
树上有只程序猿6 小时前
2026低代码选型指南,主流低代码开发平台排名出炉
前端·后端
高端章鱼哥6 小时前
为什么说用OpenClaw对打工人来说“不划算”
前端·后端