测试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. 升级到VS2013常见问题

    问题1: Building an MFC project for a non-Unicode character set is deprecated 解决方法: 用于多字节字符编码 (MBCS) 的 ...

  2. hdu1116 Play on Words--并查集

    原题链接: pid=1116">http://acm.hdu.edu.cn/showproblem.php? pid=1116 一:原题内容 Problem Description S ...

  3. springboot-quartz普通任务与可传参任务

    两者区别与作用: 普通任务:总调度(SchedulerFactoryBean)--> 定时调度器(CronTriggerFactoryBean) --> 调度明细自定义执行方法bean(M ...

  4. 用sp_executesql执行动态SQL语句及获得返回值

    过去我执行拼凑出来的动态SQL语句,都直接使用EXEC @sql 的方式.有好几次,都看到有资料说,应该尽量使用 sp_executesql. 究其原因,是因为仅仅参数不同的情况下,sp_execut ...

  5. luogu2161 [SHOI2009]会场预约

    题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...

  6. 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。

    请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...

  7. uoj#149

    dp 没想出来 最先开始想 dp[i][j][k]表示s匹配到i,t匹配到j,当前分了k段的方案数 s[i]==t[j] dp[i][j][k]+=dp[i-1][j-1][k-1] s[i]==t[ ...

  8. curl强制下载文件

    <?phpfunction download_remote_file_with_curl($file_url, $save_to) { $ch = curl_init(); curl_setop ...

  9. SpringAOP使用注解实现5种通知类型

    spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...

  10. create-react-app 引入ant design 及 使用 less

    全局引入: 第一步:全局安装 create-react-app npm install create-react-app -g 第二步:安装 yarn npm install -g yarn 第三步: ...