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

2025-05-27 0 15

本文研究的主要是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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring+Junit4进行接口测试实例代码 https://www.kuaiidc.com/76484.html

相关文章

发表评论
暂无评论