转载自:http://blog.csdn.net/radic_feng/article/details/6740438

我们期望能像在产品代码中一样,在测试用例中使用的bean也由Spring Context来管理,这样既可以充分利用Spring IOC的优势,同时又使得测试代码更像产品代码,有更好的测试效果。那么怎么能在运行测试用例时自动启动Spring Contex,从而向测试用例中注入beant呢?本文提供使用JUnitTestNG两个测试框架启动Spring
Context的步骤。

JUnit框架

JUnit对Spring有天然的支持,只需添加两条Annotation就可以启动Spring Context了,示例代码如下:

[java] view
plain
copy

  1. @ContextConfiguration("/META-INF/spring/integration/inbound-gateway-config.xml")
  2. @RunWith(SpringJUnit4ClassRunner.class)
  3. public class InboundGatewayTests {
  4. @Autowired
  5. private SimpleWebServiceInboundGateway gateway;
  6. }

仅仅添加@ContextConfiguration,指定Spring Context配置文件的位置以及@RunWith,即可使用指定配置文件中定义的bean。

TestNG框架

如果你的Eclipse还没有安装TestNG插件,请参考安装文档

与JUnit不同,TestNG没有提供@RunWith注解。TestNG的测试用例需要继承org.springframework.test.context.testng.AbstractTestNGSpringContextTests或org.springframework.test.context.testng.AbstractTestNGSpringContextTests来启动SpringContext,示例代码如下:

[java] view
plain
copy

  1. @ContextConfiguration(locations={"/WEB-INF/accountContext.xml"})
  2. public class TestDao extends AbstractTestNGSpringContextTests {
  3. @Autowired
  4. JdbcTemplate jdbcTemplate;
  5. @Test
  6. public void init() throws ServletException {
  7. try {
  8. jdbcTemplate.execute("create table contacts (name varchar(45),email varchar(45),phone varchar(45))");
  9. List list = jdbcTemplate.queryForList("select * from contacts");
  10. } catch (Exception e) {
  11. e.printStackTrace(System.out);
  12. }
  13. }
  14. }

以下讲解有关@ContextConfiguration参数的设置:

1. 不指定Context文件,@ContextConfiguration

Spring的ContextLoader会判断是否需要加载context,如果需要,默认加载器(GenericXmlContextLoader )会根据用例类名生成一个Spring Context配置文件的名字,例如,如果测试用例为com.example.MyTest,则GenericXmlContextLoader会从“classpath:/com/example/MyTest-context.xml”中加载Context。

2.指定多个Context文件,比如@ContextConfiguration("/META-INF/spring/integration/inbound-gateway-config.xml")

3.指定多个Context文件,比如@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})

ContextLoader会从classpath的根目录加载以上文件。

4. 继承关系对Context文件加载的影响,inheritLocations的使用。

[java] view
plain
copy

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations={"/base-context.xml"})
  3. public class BaseTest {
  4. // class body...
  5. }
  6. @ContextConfiguration(locations={"/extended-context.xml"})
  7. public class ExtendedTest extends BaseTest {
  8. // class body...
  9. }

在以上代码中ExtendedTest会加载“/base-context.xml” 和“/extended-context.xml”两个文件,因为inheritLocations的默认值是true。

通过以上解释,我们可以看到Spring Context文件是基于classpath来定位的。在Eclipse下,classpath的设定往往和build环境下不同,怎么将build环境下的Context文件设定到@ContextConfiguration中呢?步骤如下:

  1. 点击Run->Run Configurations...
  2. 选择你要运行的测试用例的运行配置项,比如InContainerTests,然后点击User Entries,然后点击Advanced,在弹出的窗口中选择Add External Folder,点击确定后可以选择任意目录。你可以基于Context文件相对于该目录的位置配置@ContextConfiguration中的参数。


在Eclipse中开发使用Spring IOC的JUnit/TestNG测试用例之详解的更多相关文章

  1. 如何在Eclipse中正确安装Jetty插件并初步使用(图文详解)

    不多说,直接上干货! 最近在做一个Storm项目,需要用到Jetty来进行展示.它类似于Tomcat. 一.eclipse中jetty插件安装 打开eclipse,依次点击菜单Help->Ecl ...

  2. 如何在Eclipse/Myeclipse/Scala IDEA for Eclipse 中正确删除已经下载过的插件(图文详解)

    不多说,直接上干货! 见 Eclipse/Myeclipse/Scala IDEA for Eclipse里两种添加插件的方法(在线和离线) 第一步 :在菜单栏中,找到help-------insta ...

  3. Spring IOC -bean对象的生命周期详解

    生命周期执行的过程如下:1) spring对bean进行实例化,默认bean是单例2) spring对bean进行依赖注入3) 如果bean实现了BeanNameAware接口,spring将bean ...

  4. 老李分享:Eclipse中开发性能测试loadrunner脚本

    老李分享:Eclipse中开发性能测试loadrunner脚本 前篇我分享了如何用loadrunner搭建javauser的性能测试脚本环境,本次我来告诉大家如何在eclipse开发loadrunne ...

  5. 在Eclipse中开发C/C++项目

    摘要:通过本文你将获得如何在Eclipse平台上开发C/C++项目的总体认识.虽然Eclipse主要被用来开发Java项目,但它的框架使得它很容易实现对其他开发语言的支持.在这篇文章里,你将学会如何使 ...

  6. Spring IOC 方式结合TESTGN测试用例,测试简单java的命令模式

    java命令模式: 可以命令("请求")封装成一个对象,一个命令对象通过在特定的接收着上绑定一组动作来封装一个请求.命令对象直接把执行动作和接收者包进对象中,只对外暴露出执行方法的 ...

  7. Spring、SpringMVC、SpringData + JPA 整合详解

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...

  8. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  9. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)

    通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...

随机推荐

  1. Scrum立会报告+燃尽图(十一月十九日总第二十七次):功能开发与修复上一阶段bug

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  2. 互评Alpha版本——杨老师粉丝群——Pinball

    一.基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 成语学习对除汉语言专业外的大学生的需求并不是很高,初中生和高中生因为在升学时需要参加语文 ...

  3. 在html中怎么格式化输出json字符串

    #今天的项目用到,看俊哥找到,特此记录下来 步骤: 1.在html页面中输入下面的标签,必须是在pre标签内输出格式才会生效: <pre id="songReqJson"&g ...

  4. SDN练习一

    SDN练习第一题 题目描述 实现网络拓扑: 具体要求: 南向接口采用OpenFlow 协议. 可查看网络的拓扑信息视图. H1.H2.H3.H4 任意两两可互通. 实现思路 利用mininet可视化图 ...

  5. ImportError: No module named examples.tutorials.mnist

    Traceback (most recent call last):   File "nearest_neighbor.py", line 14, in <module> ...

  6. phpdisk 盲注 &前台任意用户登录

    代码审核 文件 plugins\phpdisk_client\passport.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $str ...

  7. jQuery之_元素滚动

    对应的知识点铺垫,但是有一个很重要的问题就是兼容IE和chorme的 1. scrollTop(): 读取/设置滚动条的Y坐标2. $(document.body).scrollTop()+$(doc ...

  8. ant 安装及基础教程 !

    这篇文章主要介绍了ant使用指南详细入门教程,本文详细的讲解了安装.验证安装.使用方法.使用实例.ant命令等内容,需要的朋友可以参考下   一.概述 ant 是一个将软件编译.测试.部署等步骤联系在 ...

  9. [微软官网]One Windows Kernel

    One Windows Kernel https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/One-Windows-Kerne ...

  10. linux的一些机制Signal, Fork,

    signal(SIGCHLD, SignalHandler); 注册软中断,对应的api close(socket); ret=fork(): 父进程,返回子进程的pid. 子进程返回0, 出错返回& ...