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();
	}
}
相关推荐
CircleMouse2 分钟前
springboot项目中使用Java 8的日期时间API
java·开发语言·spring boot·后端·spring
Mr YiRan14 分钟前
C++语言学习之面向对象
java·c++·学习
UrbanJazzerati21 分钟前
Python 导包、分包完全教程
后端·面试
dc_001225 分钟前
“mysqld --initialize --console ”执行不成功情况总结和解决措施
java
摘星编程1 小时前
解锁Agent智能体的未来:五大实战策略彻底革新人机协作模式
java·开发语言
百块富翁1 小时前
可管控、不重复TraceId解决方案
java·分布式·系统架构
金銀銅鐵1 小时前
浅解 Junit 4 第二篇: Runner 和 ParentRunner
java·junit·单元测试
Aerkui1 小时前
Go 泛型(Generics)详解
开发语言·后端·golang
_codemonster1 小时前
JavaWeb开发系列(七)表单开发
java