Junit在SSH中的集成测试

测试Spring容器

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

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration(locations = { "/applicationContext.xml",
 3         "/daoContext.xml" })
 4 public class TestSpring extends AbstractJUnit4SpringContextTests {
 5 
 6     @Autowired
 7     private ApplicationContext appContext;
 8 
 9     @Autowired
10     private SessionFactory sessionFactory;
11 
12     @Test
13     public void testSpringContext() {
14         EmpManager empMgr = (EmpManager) appContext.getBean("empManager");
15         List<AttendType> types = empMgr.getAllType();
16         for (AttendType type : types) {
17             System.out.println(type.getId() + "," + type.getAmerce());
18         }
19     }
20 }

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

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

 

测试struts 的Action

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

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

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

 1 public class TestLoginAction extends StrutsSpringTestCase {
 2 
 3     @Override
 4     protected String[] getContextLocations() {
 5         return new String[] { "classpath*:applicationContext.xml",
 6                 "/daoContext.xml" };
 7     }
 8 
 9     public void testALogin() throws Exception {
10 
11         request.setParameter("manager.name", "oracle");
12         request.setParameter("manager.pass", "oracle");
13         request.setParameter("vercode", "123456");
14 
15         ActionProxy proxy = getActionProxy("/processLogin");
16         LoginAction loginAction = (LoginAction) proxy.getAction();
17 
18         // setup httpsession before invoking Action to make it equals to the
19         // vercode code
20         Map<String, Object> httpSession = new HashMap<>();
21         httpSession.put("rand", "123456");
22         ServletActionContext.getContext().setSession(httpSession);
23         // inject beans for Action properties before executing Action.execute()
24         EmpManager mgr = (EmpManager) applicationContext.getBean("empManager");
25         loginAction.setEmpManager(mgr);
26 
27         String result = proxy.execute();
28         assertTrue("result=" + result + "|"
29                 + loginAction.getActionMessages().toString(),
30                 result.equals("mgr"));
31         // assertTrue(ServletActionContext.getPageContext().getException().getMessage().toString(),result.equals("mgr"));
32 
33     }
34 
35     public void testPunchAction() throws Exception {
36         EmpManager empMgr = (EmpManager) applicationContext.getBean("empManager");
37         List<AttendType> types = empMgr.getAllType();
38         for (AttendType type : types) {
39             System.out.println(type.getId() + "," + type.getAmerce());
40         }
41     }
42 }

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

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

 

转载于:https://www.cnblogs.com/fysola/p/6720451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值