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();
	}
}
相关推荐
江畔柳前堤5 小时前
roLabelImg 详细安装教程
开发语言·人工智能·后端·云原生
白鸽(二般)6 小时前
MinIO Java Client API
java·开发语言
软萌萌的17 小时前
Java Spring Boot 修改yml配置&加载顺序规则
java·spring boot·python
陈随易7 小时前
moon,apt和yum之外linux系统命令安装新选择
前端·后端·程序员
IT小盘7 小时前
13-企业Prompt模板-角色任务约束与输出格式
java·前端·prompt
爱敲键盘的猴子8 小时前
Java并发编程 -- volatile
java·java并发
码事漫谈8 小时前
当单库日增 4.5TB,异构实时同步还能稳得住吗?
后端
plainGeekDev9 小时前
工厂模式 → Hilt
android·java·kotlin
plainGeekDev9 小时前
Application 单例 → Hilt Singleton
android·java·kotlin
888CC++10 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++