Junit在SSH中的集成测试
测试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中的集成测试的更多相关文章
- 一文教会你如何在 Spring 中进行集成测试,太赞了
不得不说,测试真的是太重要了!但并不是所有的开发者都这样认为,这种感觉在我回到洛阳后尤其强烈.竟然有团队成员不经测试就把代码提交到代码库,并且是会报错的那种,我天呐,遇到这种队友我也是醉了. 我之前是 ...
- SSH中的jar包讲解(1)
我们在搭建SSH框架的时候,需要引入各自的一些jar包,相信很多初学者跟我一样,搜个资料,照搬过来(当然版本还得对应),至于为什么要引入这些个jar包,引入它们的作用是啥子,一头雾水,今天我就来跟这些 ...
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
- ssh 中 远程文件传输
scp 命令是 SSH 中最方便有用的命令了,试想,在两台服务器之间直接传送文件,仅仅用 scp 一个命令就完全解决了. 你可以在一台服务器上 以 root 身份运行 #scp servername: ...
- ssh中的相对路径与绝对路径的问题
一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...
- junit在idea中的使用(1)--理论篇
感觉本文前部分配置太过繁琐,大家可以参考我的这篇文章http://www.cnblogs.com/SuMeng/p/8279879.html(junit在IDEA中使用--实践篇),用添加maven ...
- SSH中的Hibernate
SSH中的Hibernate 就是DAO连接数据库对数据进行实际操作,做了架构简化,对数据库的操作.
- 【转】SSH中 整合spring和proxool 连接池
[摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...
- Junit使用过程中需要注意的诡异bug以及处理办法
在开发过程中我们有时会遇到狠多的问题和bug,对于在编译和运行过程中出现的问题很好解决,因为可以在错误日志中得到一定的错误提示信息,从而可以找到一些对应的解决办法.但是有时也会遇到一些比较诡异的问题和 ...
随机推荐
- Android 输入管理服务-输入事件到达之后的处理流程
接上一篇博客"Android 输入管理服务启动过程的流程".这两天分析了Android 输入管理服务接收到输入事件之后的处理流程,详细流程例如以下面两图所看到的: 接下图
- Lucene 4.0 TieredMergePolicy
默认的merge policy. findMerges: 先将全部段依照扣除删除文档之后的字节数(bytesize * (1.0 - delRatio))降序排,对当中size() > 2.5G ...
- SGU - 403 - Scientific Problem (水)
403. Scientific Problem Time limit per test: 0.25 second(s) Memory limit: 65536 kilobytes input: sta ...
- linux下获取按键响应事件【转】
本文转载自:https://my.oschina.net/u/157503/blog/91548 1.问题 通过一个死循环将读取键盘对应的设备文件将触发键盘事件在屏幕上打印出来,按esc退出程序 代码 ...
- Spark底层原理简化版
目录 Spark SQL/DF的执行过程 集群运行部分 Aggregation Join Shuffle Tungsten 内存管理机制 缓存敏感计算(Cacheaware computation) ...
- iTex导出PDF
iText导出PDF,所需jar包如下: itext-asian-5.2.0.jar 支持导出中文的jar包 itextpdf-5.5.9.jar PDF核心jar包 bcprov-jdk15on-1 ...
- Spring Cloud (1) 服务的注册与发现(Eureka)
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...
- css的外边距合并或者外边距塌陷问题
第一种情况: 已知两个宽和高均为100px,margin均为20px的div垂直排列,现象如下图所示: 当设置css1的margin-bottom:40px:或者css2的margin-top:40p ...
- SEO之如何做301转向
1.如果网站使用的是(Linux+Apache+MySQL+PHP)主机,可以使用.htaccess文件做301转向 比如把/index.html 301转向到http://www.xinlvtian ...
- Mysql Event 自动分表
create table TempComments Like dycomments; 上述 SQL语句创建的新表带有原表的所有属性,主键,索引等. 自动分表怎么做呢? 使用上述语句自动创建分表. 那么 ...