测试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. 《CS:APP》 chapter 9 Vitrual Memory 笔记

    Vitrual Memory In order to manage memory more efficiently and with fewer errors, modern systems prov ...

  2. [洛谷0925]NOIP模拟赛 个人公开赛 OI

     P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个 ...

  3. K度限制MST poj 1639

    /* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了 ...

  4. bzoj2120 数颜色——带修莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...

  5. iOS手势识别

    一.手势识别与触摸事件 1.如果想监听一个view上面的触摸事件,可选的做法是: (1)自定义一个view (2)实现view的touches方法,在方法内部实现具体处理代码 2.通过touches方 ...

  6. git的使用(转)

    git 配置文件 git的配置文件位置针对所有用户:/etc/gitconfig针对当前用户: -/.gitconfig 查看配置的方法 git config --list 修改配置的方法 git c ...

  7. Prime Path(bfs)

    http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素 ...

  8. BZOJ 2084 二分+hash OR Manacher

    思路: 二分+哈希 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  9. A - Voting(queue)

    Problem description There are n employees in Alternative Cake Manufacturing (ACM). They are now voti ...

  10. SQLServer2008 关于CASE WHEN

    CASE WHEN的两种格式 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE ...