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();
	}
}
相关推荐
hackhase7 分钟前
Django 多表数据整合:7 张异构表如何收敛成 4 类页面
后端·mysql
用户3721574261351 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java
Cosolar2 小时前
需求做完了,经验归零了:Agent 真正缺的是第二条闭环
人工智能·后端·agent
用户3721574261353 小时前
Java 将 Excel 图表导出为图片:指定图表、批量导出与图表工作表
java
这个昵称不容易3 小时前
交流论坛网站设计与实现:SpringBoot 论坛 130 个接口,附论文与运行截图
java·springboot·毕设
CRZZX4 小时前
Agent 上下文管理与可观测性设计
后端·agent
yAdrian4 小时前
将 milkv-duo-s 开发板变成 AP
后端·嵌入式
秋天的怀念_David4 小时前
算法一千零一夜 · 第三十七夜 · 类型的过路费
java·程序人生·算法
apcipot_rain4 小时前
C++转java日记(13)
java·开发语言