首页 > 编程 > Java > 正文

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

2019-11-26 10:21:08
字体:
来源:转载
供稿:网友

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

总结

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表