测试Spring容器

在Junit的测试类中,继承AbstractJUnit4SpringContextTests就可以进行Spring容器测试, 例如下面测试用例,

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml",
"/daoContext.xml" })
public class TestSpring extends AbstractJUnit4SpringContextTests { @Autowired
private ApplicationContext appContext; @Autowired
private SessionFactory sessionFactory; @Test
public void testSpringContext() {
EmpManager empMgr = (EmpManager) appContext.getBean("empManager");
List<AttendType> types = empMgr.getAllType();
for (AttendType type : types) {
System.out.println(type.getId() + "," + type.getAmerce());
}
}
}

在AbstractJUnit4SpringContextTests中自带一个applicationContext属性变量,默认使用applicationContext.xml进行初始化,

在子类中可以在类上重新指定新的配置文件,并再次定义ApplicationContext私有成员, 从而在测试方法中使用,如上。

测试struts 的Action

struts2提供了StrutsSpringTestCase这个类用来在junit中做测试, 只需要将测试类继承此类即可,

在测试方法中,将使用代理方法执行Action

在测试类中,通过重写 getContextLocations方法,可以自定义配置文件,如下,

 public class TestLoginAction extends StrutsSpringTestCase {

     @Override
protected String[] getContextLocations() {
return new String[] { "classpath*:applicationContext.xml",
"/daoContext.xml" };
} public void testALogin() throws Exception { request.setParameter("manager.name", "oracle");
request.setParameter("manager.pass", "oracle");
request.setParameter("vercode", "123456"); ActionProxy proxy = getActionProxy("/processLogin");
LoginAction loginAction = (LoginAction) proxy.getAction(); // setup httpsession before invoking Action to make it equals to the
// vercode code
Map<String, Object> httpSession = new HashMap<>();
httpSession.put("rand", "123456");
ServletActionContext.getContext().setSession(httpSession);
// inject beans for Action properties before executing Action.execute()
EmpManager mgr = (EmpManager) applicationContext.getBean("empManager");
loginAction.setEmpManager(mgr); String result = proxy.execute();
assertTrue("result=" + result + "|"
+ loginAction.getActionMessages().toString(),
result.equals("mgr"));
// assertTrue(ServletActionContext.getPageContext().getException().getMessage().toString(),result.equals("mgr")); } public void testPunchAction() throws Exception {
EmpManager empMgr = (EmpManager) applicationContext.getBean("empManager");
List<AttendType> types = empMgr.getAllType();
for (AttendType type : types) {
System.out.println(type.getId() + "," + type.getAmerce());
}
}
}

需要注意的是上面的第25行的目的是为Action类中的属性注入bean, 因为我这里的Action bean并没有放在Spring容器中托管, 在实际项目中,struts框架会自动装配Action的属性, 但在这里需要手工装配。

另外继承了StrutsSpringTestCase的测试类,不仅可以测试Action,同时也能测试Spring容器,例如上面的testPunchAction()方法。

Junit在SSH中的集成测试的更多相关文章

  1. 一文教会你如何在 Spring 中进行集成测试,太赞了

    不得不说,测试真的是太重要了!但并不是所有的开发者都这样认为,这种感觉在我回到洛阳后尤其强烈.竟然有团队成员不经测试就把代码提交到代码库,并且是会报错的那种,我天呐,遇到这种队友我也是醉了. 我之前是 ...

  2. SSH中的jar包讲解(1)

    我们在搭建SSH框架的时候,需要引入各自的一些jar包,相信很多初学者跟我一样,搜个资料,照搬过来(当然版本还得对应),至于为什么要引入这些个jar包,引入它们的作用是啥子,一头雾水,今天我就来跟这些 ...

  3. Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用

    ==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...

  4. ssh 中 远程文件传输

    scp 命令是 SSH 中最方便有用的命令了,试想,在两台服务器之间直接传送文件,仅仅用 scp 一个命令就完全解决了. 你可以在一台服务器上 以 root 身份运行 #scp servername: ...

  5. ssh中的相对路径与绝对路径的问题

    一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...

  6. junit在idea中的使用(1)--理论篇

     感觉本文前部分配置太过繁琐,大家可以参考我的这篇文章http://www.cnblogs.com/SuMeng/p/8279879.html(junit在IDEA中使用--实践篇),用添加maven ...

  7. SSH中的Hibernate

    SSH中的Hibernate 就是DAO连接数据库对数据进行实际操作,做了架构简化,对数据库的操作.

  8. 【转】SSH中 整合spring和proxool 连接池

    [摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...

  9. Junit使用过程中需要注意的诡异bug以及处理办法

    在开发过程中我们有时会遇到狠多的问题和bug,对于在编译和运行过程中出现的问题很好解决,因为可以在错误日志中得到一定的错误提示信息,从而可以找到一些对应的解决办法.但是有时也会遇到一些比较诡异的问题和 ...

随机推荐

  1. 2本Hadoop技术内幕电子书百度网盘下载:深入理解MapReduce架构设计与实现原理、深入解析Hadoop Common和HDFS架构设计与实现原理

    这是我收集的两本关于Hadoop的书,高清PDF版,在此和大家分享: 1.<Hadoop技术内幕:深入理解MapReduce架构设计与实现原理>董西成 著  机械工业出版社2013年5月出 ...

  2. UVA 11021 - Tribles(概率递推)

    UVA 11021 - Tribles 题目链接 题意:k个毛球,每一个毛球死后会产生i个毛球的概率为pi.问m天后,全部毛球都死亡的概率 思路:f[i]为一个毛球第i天死亡的概率.那么 f(i)=p ...

  3. bzoj4004 [JLOI2015]装备购买——线性基+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4004 今天讲课讲到的题,据说满足拟阵的性质,所以贪心是正确的: 总之就贪心,按价格从小到大排 ...

  4. 软件-版本控制:VCS(版本控制系统)

    ylbtech-软件-版本控制:VCS(版本控制系统) 版本控制系统(Version Control System),是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统 ...

  5. cors解决跨越问题

    转载于http://www.cnblogs.com/jiangwz/p/8142740.html Cross-Origin Resource Sharing(CORS)跨来源资源共享是一份浏览器技术的 ...

  6. 数据通讯与网络 第五版第24章 传输层协议-TCP协议部分要点

    上一博客记录了UDP协议的关键要点,这部分记录TCP协议的关键要点. 24.3 传输控制协议(TRANSMISSION CONTROL PROTOCOL) TCP(Transmission Contr ...

  7. B - Bit++

    Problem description The classic programming language of Bitland is Bit++. This language is so peculi ...

  8. 分布式文件管理系统MooseFS在centOS 7中的安装

    首先,MooseFS是做什么的在这边不做具体详述,这边主要记录一下我在自己部署MooseFS中遇到的问题和步骤(大部分参考的其他博客或者资料) 首先是准备资源,MooseFS的最新安装包可以去官网下载 ...

  9. java ---书写自己的名字

    public class hello { public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println(& ...

  10. Deutsch lernen (01)

    Was macht Martin? - Um 8.00 Uhr steht martin auf. aufstehen - aufstand - ist aufgestanden 起床 Um 6 Uh ...