Spring+Junit4进行接口测试实例代码
作者:Wind_365 时间:2021-09-15 07:44:06
本文研究的主要是Spring+Junit4进行接口测试的一个相关实例,具体实现代码如下。
1.配置pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.配置bean
testInterface可以进行手动配置或自动扫描
手动配置
spring配置文件配置:
<bean id="testInterface" class="com.xxx.TestInterfaceImpl">
</bean>
自动扫描
接口实现类中配置
@Component
public class TestInterfaceImpl implements TestInterface {
spring配置文件配置
<context:annotation-config/>
<context:component-scan base-package="com.xxx.servlet">
</context:component-scan>
3.编写接口测试代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test {
@Resource
TestInterface testInterface;
@Test
public void test1(){
testInterface.test1(1,2);
}
总结
来源:http://blog.csdn.net/fengqingting2/article/details/52650126
标签:spring,junit4,接口测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
原来Java中有两个ArrayList
2023-06-27 11:49:40
spring data jpa分页查询示例代码
2023-01-09 14:51:50
详解Java使用Pipeline对Redis批量读写(hmset&hgetall)
2023-11-17 15:17:24
Android实现带动画效果的可点击展开TextView
2023-08-06 05:42:08
![](https://img.aspxhome.com/file/2023/9/85989_0s.png)
Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)
2023-03-21 15:11:38
WinForm下 TextBox只允许输入数字的小例子
2023-08-02 15:34:14
java微信公众号企业付款开发
2023-04-07 00:21:08
![](https://img.aspxhome.com/file/2023/1/88041_0s.jpg)
kafka消费不到数据的排查过程
2023-11-14 04:35:30
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
java gui详解贪吃蛇小游戏实现流程
2023-10-08 07:12:24
![](https://img.aspxhome.com/file/2023/0/78920_0s.png)
SpringBoot结合JWT登录权限控制的实现
2023-10-06 04:54:22
java实现百度云OCR文字识别 高精度OCR识别身份证信息
2023-10-24 13:50:37
![](https://img.aspxhome.com/file/2023/1/82441_0s.png)
java中带参数的try(){}语法含义详解
2021-10-27 05:20:16
![](https://img.aspxhome.com/file/2023/1/76211_0s.jpg)
springboot2.x整合tkmapper的示例代码
2021-09-19 11:56:59
grails不能运行fork模式解决方法
2023-05-17 18:37:59
Spring Boot2中如何优雅地个性化定制Jackson实现示例
2021-09-27 12:21:15
手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程
2022-08-22 00:15:25
![](https://img.aspxhome.com/file/2023/0/74980_0s.jpg)
关于Struts2文件上传与自定义拦截器
2021-10-31 12:52:44
SpringAOP 如何通过JoinPoint获取参数名和值
2023-11-01 00:50:36
![](https://img.aspxhome.com/file/2023/1/69351_0s.png)
jenkins+Maven从SVN上构建项目的方法
2022-07-09 04:42:37
![](https://img.aspxhome.com/file/2023/2/61582_0s.png)