• 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()方法。

  • 相关阅读:
    Symantec Backup Exec Remote Agent 2010在Redhat Enterprise 6.6上启动问题
    RMAN冷备份异机还原
    ORACLE OLAP错误ORA-06512: at "SYS.OLAPIHISTORYRETENTION"
    expdp 报The value (30) of MAXTRANS parameter ignored错误的原因诊断
    ORA-01157 & ORA-01110
    INITIAL参数设置导致TRUNCATE TABLE不能降低高水位线案例
    ORA-19563: header validation failed for file
    ORACLE NUMBER类型Scale为0引发的问题
    ORA-04063: view "SYS.DBA_REGISTRY" has errors
    Linux 格式化扩展分区(Extended)
  • 原文地址:https://www.cnblogs.com/fysola/p/6720451.html
Copyright © 2020-2023  润新知