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();
	}
}
相关推荐
SnrtIevg3 小时前
Spring Modulith :构建模块化单体应用
后端
LSTM973 小时前
用 Python 自动化编辑 Word 文档
后端
毕设源码-郭学长4 小时前
【开题答辩全过程】以 基于java的校园安全管理系统为例,包含答辩的问题和答案
java·开发语言
王中阳Go4 小时前
手把手教你用 GoFrame 实现 RBAC 权限管理,从零到一搞定后台权限系统
后端
苏三说技术4 小时前
try...catch真的影响性能吗?
后端
青梅主码4 小时前
麦肯锡发布最新报告《职场超级代理:赋能人们释放 AI 的全部潜力》:如何用 AI 赋能员工,释放无限潜力?
后端
雨中飘荡的记忆4 小时前
拼团系统设计与实现
java·spring boot
青云交4 小时前
Java 大视界 -- Java 大数据在智能医疗影像数据标注与疾病辅助诊断模型训练中的应用
java·大数据·多模态融合·医疗影像标注·辅助诊断·临床 ai·dicom 处理
雨中飘荡的记忆4 小时前
Step Builder模式实战
java·设计模式
悦来客栈的老板4 小时前
AST反混淆实战|reese84_jsvmp反编译前的优化处理
java·前端·javascript·数据库·算法