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();
	}
}
相关推荐
chilavert31824 分钟前
技术演进中的开发沉思-368:锁机制(中)
java·开发语言·jvm
~央千澈~36 分钟前
抖音弹幕游戏开发之第12集:添加冷却时间机制·优雅草云桧·卓伊凡
java·服务器·前端
HAPPY酷1 小时前
C++ 多线程实战三板斧
java·开发语言·c++·技术美术
独自破碎E1 小时前
BISHI54货物堆放
android·java·开发语言
json{shen:"jing"}1 小时前
分割回文串
java
workflower1 小时前
易用性和人性化需求
java·python·测试用例·需求分析·big data·软件需求
无心水2 小时前
5、微服务快速启航:基于Pig与BladeX构建高可用分布式系统实战
服务器·分布式·后端·spring·微服务·云原生·架构
小灵不想卷2 小时前
LangChain4 初体验
java·langchain·langchain4j
忍者必须死2 小时前
ConcurrentHashMap源码解析
java
顾北122 小时前
SpringCloud 系列 04:Gateway 断言 / 过滤器 / 限流 一站式落地指南
java·开发语言·数据库