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();
	}
}
相关推荐
摇滚侠34 分钟前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
GIS数据转换器42 分钟前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
华如锦1 小时前
面了很多 Java转AI Agent方向,一些面试题总结
java·开发语言·人工智能·python·ai
睡不醒男孩0308231 小时前
CLup 6.x 版本中针对StarRocks 存算一体集群的完整操作手册
java·服务器·网络·clup
程序员黑豆2 小时前
Java中怎么实现字符串拼接呢【AI全栈开发】
java
fox_lht2 小时前
15.3.改进我们之前的输入、输出项目
开发语言·后端·学习·rust
大鸡腿同学3 小时前
用 AI 肝了一个星期的智能客服助手,看看怎么个事
后端
IT_陈寒3 小时前
Python的os.path.join居然能这么坑?
前端·人工智能·后端
java1234_小锋3 小时前
LangChain4j 开发Java Agent智能体- 多模态支持
java·开发语言·langchain4j