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();
	}
}
相关推荐
geovindu17 分钟前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
梦梦代码精1 小时前
电商系统不是技术堆叠:LikeShop如何用分层Hold住复杂业务?
java·docker·代码规范
负责的蛋挞1 小时前
异步HttpModule的实现方式
java·服务器·前端
AC赳赳老秦1 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw
Tian_Hang2 小时前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
卷无止境2 小时前
C++ 存储类说明符(Storage Class Specifier)大横评
c++·后端
用户019027581612 小时前
量化数据的 batch 接口有多好用?从 1 只到 500 只,批量拉数据的正确姿势
后端
rruining2 小时前
Java设计模式——结构型
后端
卷无止境2 小时前
C++ 编程的一大坑:非常量全局变量是"万恶之源"
c++·后端
Mr-Wanter3 小时前
wsl2 jdk管理工具之sdkman
java·开发语言·sdkman