服务器之家

服务器之家 > 正文

Spring+Junit4进行接口测试实例代码

时间:2021-03-29 10:24     来源/作者:Wind_365

本文研究的主要是Spring+Junit4进行接口测试的一个相关实例,具体实现代码如下。

1.配置pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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配置文件配置:

?
1
2
<bean id="testInterface" class="com.xxx.TestInterfaceImpl">
  </bean>

自动扫描

接口实现类中配置

?
1
2
@Component
public class TestInterfaceImpl implements TestInterface {

spring配置文件配置

?
1
2
3
4
5
<context:annotation-config/>
 
 
  <context:component-scan base-package="com.xxx.servlet">
  </context:component-scan>

3.编写接口测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
  }

总结

以上就是本文关于Spring+Junit4进行接口测试实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/fengqingting2/article/details/52650126

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部