java 测试action_基于StrutsTestCase的单元测试(Action测试方法)

1.packagetest.sample.service.util;2.importjava.io.IOException;3.importjava.util.HashMap;4.importjavax.servlet.http.HttpServletRequest;5.importjavax.servlet.http.HttpServletResponse;6.importjavax.servlet.http.HttpSession;7.8.importorg.apache.struts.action.Action;9.importorg.apache.struts.action.ActionForm;10.importorg.apache.struts.action.ActionMapping;11.importorg.apache.struts.action.RequestProcessor;12.importorg.apache.struts.util.RequestUtils;13.14./***//**15.  *@authoradministrator

16.  *

17.  * To change this generated comment edit the template variable "typecomment":

18.  * Window>Preferences>Java>Templates.

19.  * To enable and disable the creation of type comments go to

20.  * Window>Preferences>Java>Code Generation.

21.*/22.publicclassSTCRequestProcessorextendsRequestProcessor {23.24.privatestaticHashMap mockActionFormMap=newHashMap();25.privatestaticHashMap mockActionMap=newHashMap();26.27.publicstaticvoidaddMockAction(String actionStr, String className)28.     {29.         mockActionMap.put(actionStr, className);30.     }31.publicstaticvoidaddMockActionForm(String actionFormStr,String className)32.     {33.         mockActionFormMap.put(actionFormStr, className);34.     }35./***//**36.      * We will insert Mock ActionForm for testing through this method

37.*/38.protectedActionForm processActionForm(39.         HttpServletRequest request,40.         HttpServletResponse response,41.         ActionMapping mapping) {42.43.44.//Create (if necessary a form bean to use45.         String formBeanName=mapping.getName();46.         String mockBeanClassName=(String) mockActionFormMap.get(formBeanName);47.if(mockBeanClassName==null)48.returnsuper.processActionForm(request, response, mapping);49.50.         ActionForm instance=null;51.try{52.             Class formClass=Class.forName(mockBeanClassName );53.             instance=(ActionForm) formClass.newInstance();54.         }catch(ClassNotFoundException e) {55.             e.printStackTrace();56.         }catch(InstantiationException e) {57.             e.printStackTrace();58.         }catch(IllegalAccessException e) {59.             e.printStackTrace();60.         }61.62.         instance.setServlet(servlet);63.if(instance==null) {64.return(null);65.         }66.67.if(log.isDebugEnabled()) {68.             log.debug(69."Storing ActionForm bean instance in scope '"70.+mapping.getScope()71.+"' under attribute key '"72.+mapping.getAttribute()73.+"'");74.         }75.if("request".equals(mapping.getScope())) {76.             request.setAttribute(mapping.getAttribute(), instance);77.         }else{78.             HttpSession session=request.getSession();79.             session.setAttribute(mapping.getAttribute(), instance);80.         }81.return(instance);82.83.     }84.85./***//**86.      * We will insert Mock Action Class through this method

87.*/88.protectedAction processActionCreate(89.         HttpServletRequest request,90.         HttpServletResponse response,91.         ActionMapping mapping)92.throwsIOException {93.         String orignalClassName=mapping.getType();94.         String mockClassName=(String)mockActionMap.get(orignalClassName);95.if( mockClassName==null)96.returnsuper.processActionCreate(request,response,mapping);97.         String className=mockClassName;98.if(log.isDebugEnabled()) {99.             log.debug("Looking for Action instance for class"+className);100.         }101.102.         Action instance=null;103.synchronized(actions) {104.105.//Return any existing Action instance of this class106.             instance=(Action) actions.get(className);107.if(instance!=null) {108.if(log.isTraceEnabled()) {109.                     log.trace("Returning existing Action instance");110.                 }111.return(instance);112.             }113.114.//Create and return a new Action instance115.if(log.isTraceEnabled()) {116.                 log.trace("Creating new Action instance");117.             }118.119.try{120.                 instance=(Action) RequestUtils.applicationInstance(className);121.             }catch(Exception e) {122.                 log.error(123.                     getInternal().getMessage("actionCreate", mapping.getPath()),124.                     e);125.126.                 response.sendError(127.                     HttpServletResponse.SC_INTERNAL_SERVER_ERROR,128.                     getInternal().getMessage(129."actionCreate",130.                         mapping.getPath()));131.132.return(null);133.             }134.135.             instance.setServlet(this.servlet);136.             actions.put(className, instance);137.         }138.139.return(instance);140.     }141.142. }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值