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();
	}
}
相关推荐
掘金者阿豪6 小时前
被一个标量子查询折腾了两天,最后发现是数据库自己“偷了懒”
后端
NE_STOP6 小时前
Docker--容器常用命令
java
摇滚侠6 小时前
MSYS2 Builds Hashes Cygwin Builds Hashes 区别
java
武子康7 小时前
Java-08 深入浅出 Mybatis 数据库多对多关系设计:中间表、映射与性能优化
java·后端·spring
明月_清风7 小时前
二进制序列化入门——为什么二进制比文本更快、更小?
后端·protobuf·messagepack
无极低码7 小时前
wsdl转client使用wsimport,高版本openjdk不支持使用 JAX-WS
java
明夜之约7 小时前
Spring Cloud Gateway 深度解析:从路由原理到生产级网关实战
java·spring·spring cloud·gateway
咕白m6257 小时前
Excel 工作表名称读取(Python 实现)
后端·python
Simon523147 小时前
Spring Bean----5.27学习小记
java·学习·spring
雪隐7 小时前
AI股票小助手00-导言
人工智能·后端