利用JUnit及STC进行单体测试(二).ppt_第1页
利用JUnit及STC进行单体测试(二).ppt_第2页
利用JUnit及STC进行单体测试(二).ppt_第3页
利用JUnit及STC进行单体测试(二).ppt_第4页
利用JUnit及STC进行单体测试(二).ppt_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

,BeyondTechnology,利用JUnit及STC进行单体测试(二)(Ver1.0),目的与目标,StrutsTestCaseforJUnit介绍STC提供的两种测试方式STC的配置使用STC示例,课程概述,实际项目需要JUnit的不足STC的优势,StrutsTestCaseforJUnit,StrutsTestCase提供的两种途径,采用模拟对象途径采用Cactus来真实的运行Struts的ActionServlet,Cactus测试框架的配置,修改web.xml文件建立perties文件指定日志文件配置strutstest包,修改web.xml文件,a.增加一个filterFilterRedirectororg.apache.cactus.server.FilterTestRedirector增加相应的filtermappingFilterRedirector/FilterRedirectorb.增加两个servletJspRedirector/jspRedirector.jspServletRedirectororg.apache.cactus.server.ServletTestRedirector增加相应的servletmappingJspRedirector/JspRedirectorServletRedirector/ServletRedirector,建立perties文件,在当前工程文件夹下的src下,新建文件:perties,内容如下:#ConfigurationfileforCactus.#EachprojectusingCactusneedtohavesuchafileputintheclientside#CLASSPATH(Meaningthedirectorycontainginthisfileshouldbeintheclient#sideCLASSPATH,notthefileitselfofcourse.:)#DefinestheURLsthatwillbeusedbyCactustocallitsredirectors.#YouneedtospecifyintheseURLsthewebappcontextthatyouuseforyour#application.Intheexamplebelow,thecontextistest.#WebappContextunderwhichourapplicationtotestrunscactus.contextURL=http:/localhost:8080/wkrp1#DefaultServletRedirectorName.UsedbyServletTestCasetestcases.cactus.servletRedirectorName=ServletRedirector#DefaultJSPRedirectorName.UsedbyJspTestCasetestcases.cactus.jspRedirectorName=JspRedirector#NOTE:Someservlet2.3containersneedtomapthefiltertoavalid#resource(Orion1.5.2forexample).Others,likeTomcat,arehappy#withjustanymapping.#Forexample,thefollowingwouldworkwithTomcat(andthisiswhatIwould#haveexpected!):#cactus.filterRedirectorName=FilterRedirectorcactus.filterRedirectorName=/FilterRedirector#EnableCactusinternalloggingcactus.enableLogging=true1端口号应写实际你所使用的端口号。,建立log_perties文件,在当前工程文件夹下的src下,新建文件:log_perties,内容如下:#PropertiesforconfiguringLog4j#ThisistheconfiguringforloggingontheJUnitside(i.e.theclientside)log4j.appender.cactus=org.apache.log4j.FileAppenderlog4j.appender.cactus.File=d:/work/source/cactus_client.log1log4j.appender.cactus.Append=falselog4j.appender.cactus.layout=org.apache.log4j.PatternLayoutlog4j.appender.cactus.layout.ConversionPattern=%dABSOLUTE%t%-5p%-30.30c2%x-%m%n#AnyapplicationlogwhichusesLog4JwillbeloggedtotheCactuslogfilelog4j.rootCategory=DEBUG,cactus#BydefaultwedontlogattheDEBUGlevelforCactuslog,inordernottogeneratetoo#manylogs.However,shouldaproblemariseandlogsneedtobesenttotheCactusdevteam,#thenwewillaskyoutochangethistoDEBUG..apache.cactus=WARN,.apache.cactus=false1指定日志文件的位置与名称,如果指定的文件夹不存在,则会自动创建。,建立log_perties文件,在当前工程文件夹下的src下,新建文件:log_perties,内容如下:#PropertiesforconfiguringLog4j#Thisistheconfiguringforloggingontheserversidelog4j.appender.cactus=org.apache.log4j.FileAppenderlog4j.appender.cactus.File=d:/work/source/cactus_server.log1log4j.appender.cactus.Append=falselog4j.appender.cactus.layout=org.apache.log4j.PatternLayoutlog4j.appender.cactus.layout.ConversionPattern=%dABSOLUTE%t%-5p%-30.30c2%x-%m%n#AnyapplicationlogwhichusesLog4JwillbeloggedtotheCactuslogfilelog4j.rootCategory=DEBUG,cactus#BydefaultwedontlogattheDEBUGlevelforCactuslog,inordernottogeneratetoo#manylogs.However,shouldaproblemariseandlogsneedtobesenttotheCactusdevteam,#thenwewillaskyoutochangethistoDEBUG..apache.cactus=WARN,.apache.cactus=false注:2、3、4所用的文件可以直接复制strutstest例子下的文件,然后进行修改。,配置strutstest包,配置strutstest包,CactusStrutsTestCase的继承关系,CactusStrutsTestCase-ServletTestCase-AbstractTestCase-TestCase,一个简单的例子,PublicclassLogonActionpublicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)LogonActionFormactionForm=(LogonActionForm)form;StringuserName=actionForm.getUserName();Stringpassword=actionForm.getPassword();if(AppLogic.doLogon(userName,password)return(actionMapping.findForward(“success”);elsereturn(actionMapping.findForward(“failed”);,对应的actionform,PublicclasslogonFormprivateStringuserName;privateStringpassword;publicStringgetUserName()returnthis.userName;publicStringgetPassword()returnthis.password;publicvoidsetUserName(StringuserName)this.userName=userName;publicvoidgetPassword(Stringpassword)this.password=password;,相对应的测试类,publicclassLogonActionTestextendsCactusStrutsTestCase/*构造方法参数是必须的,在用TestSuite进行集合测试时需要*/publicLogonActionTest(StringtestName)super(testName);publicvoidsetUp()throwsExceptionsuper.setUp();publicvoidtearDown()throwsExceptionsuper.tearDown();,/*以下为实现一条测试用例的方法*/publicvoidtestLogonSuccessful()setRequestPathInfo(/logonAction);addRequestParameter(“userName,“zhangq”);addRequestParameter(password,“zhan

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论