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();
	}
}
相关推荐
happymaker06262 分钟前
Nexus私服的使用(配合Maven)
java·maven
JAVA学习通2 分钟前
本地知识库接入大模型时的权限隔离与安全设计
java·人工智能·安全·spring
AbandonForce3 分钟前
C++ 多态(多态定义 多态应用 多态底层||final override关键字||抽象类)
java·开发语言·c++
程序员cxuan5 分钟前
马斯克把 Cursor 给收了
人工智能·后端·程序员
Victor3566 分钟前
MongoDB(98)如何实现MongoDB的数据归档?
后端
Victor3567 分钟前
MongoDB(97)如何在MongoDB中执行分布式事务?
后端
阿丰资源8 分钟前
基于SpringBoot+MySQL+Maven的图书推荐系统项目文档(附源码)
spring boot·mysql·maven
Bat U10 分钟前
JavaEE|多线程(三)
java·前端·java-ee
卷到起飞的数分1 小时前
JVM探究
java·服务器·jvm
Geek攻城猫1 小时前
Java生产环境问题排查实战指南
java·jvm