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();
	}
}
相关推荐
wuqingshun3141591 小时前
RabbitMQ 中无法路由的消息会去到哪里?
java
北冥you鱼2 小时前
Go-Ethereum (Geth) 最佳实践:从部署到生产环境优化
开发语言·后端·golang
thefool1122662 小时前
Java 方法重载
java
程序员爱钓鱼3 小时前
Rust 函数与返回值详解:参数、表达式与返回类型
后端·rust
这不小天嘛8 小时前
JAVA八股——J集合篇
java·开发语言
什巳11 小时前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
豆瓣鸡11 小时前
RocketMQ学习-Spring Boot消息实践
java·spring boot·rocketmq
wuqingshun31415911 小时前
说一下mysql的覆盖索引
java
罗超驿12 小时前
2.算法效率的核心密码:时间复杂度和空间复杂度详解
java·数据结构·算法
栈溢出了13 小时前
Redis 分片集群与哈希槽笔记
java·redis·笔记·spring