JUnit在SSH中的集成测试

发表于:2017-5-11 10:06

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:云中之歌    来源:博客园

  测试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()方法。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号