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();
	}
}
相关推荐
烤麻辣烫几秒前
黑马程序员苍穹外卖(新手)DAY10
java·开发语言·学习·spring·intellij-idea
q***7355几秒前
Spring Boot应用关闭分析
java·spring boot·后端
u***45163 分钟前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
p***62996 分钟前
mysql-connector-java 和 mysql-connector-j的区别
android·java·mysql
程序员三明治7 分钟前
【Java】synchronized关键字详解:从字节码到对象头与锁升级
java·开发语言·juc·synchronized··锁升级
k***3888 分钟前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
y***54888 分钟前
Rust在嵌入式中的实时操作系统
开发语言·后端·rust
老虎06279 分钟前
Java基础面试题(11)—Java(泛型)
java·开发语言·windows
一 乐9 分钟前
宠物医院预约|宠物医院|基于SprinBoot+vue的宠物医院预约管理系统源码+数据库+文档)
java·前端·数据库·vue.js·后端·springboot
Seven9713 分钟前
数据结构-哈希表
java