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();
	}
}
相关推荐
程序员cxuan31 分钟前
再见 Claude,国模 SOTA 模型来了
人工智能·后端·程序员
程序员cxuan35 分钟前
夯爆了,影视界的 Codex 来了!
人工智能·后端·程序员
孟健1 小时前
抖音官方 CLI 上线:一句话搜同行爆款、扒文案,实测全记录
后端·ai编程
小手cool1 小时前
二分查找:在有序数组中查找最接近 target 的元素索引
java·数据结构·算法
烬中寻道1 小时前
GoFrame + Trae:把数据库凭据从配置文件里彻底拿掉
后端
李白的手机1 小时前
深入理解 Spring @RefreshScope:从入门用法到源码原理,再到举一反三实战扩展
java·后端·spring
深入云栈2 小时前
Netty 4.2.x 源码深度解析 (二十五):PoolThreadCache 与引用计数 —— 线程缓存与内存泄漏检测
java·后端·架构
砚底藏山河2 小时前
量化行情横截面中性化闭式与回归实现
android·java·python·金融·kotlin
想不到一个好的ID2 小时前
Redis 常见数据类型与应用场景
后端