您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 对基于Spring框架的J2EEMVCWeb组件测试
对基于Spring框架的J2EEMVC组件测试---—没有涉及HttpServletRequest等容器的对象时1、对SpringMVC的控制器组件进行测试(1)以“在JBUilder中创建Spring的MVCWeb应用+UserLogin(1).doc”一文中的控制器作为测试目标类(2)测试设计的目标----容器外的单元测试毫无疑问,我们的UserLoginController控制器类必须从HttpServletRequest中取得用户的表单数据,然而如果这么设计的话,我们的TestCase勢必与Servlet容器相依赖,因而无法独立地进行容器外的单元测试,而应该是“容器内的单元测试”。为了能独立于容器进行单元测试,我们设计了一个UserLoginForm---?,它负责存储HttpServletRequest中的表单的登录数据,而UserLoginController上拥有一个onSubmit()方法,专门负责处理UserLoginForm对象并返回一个ModelAndView对象。2、新建一个TestSpringMVCFormController项目(对前面的测试采用两个Project的方式来进行测试)3、配置该项目(1)引入Spring的系统包(2)引入被测试的项目(3)引入被测试类文件UserLoginController.java4、为前面的UserLoginController提纲一个测试用例类TestUserLoginController,springwebappttest,基类为junit.framework.TestCase再进入下一步,将出现下面的内容但JBuilder出现一个错误!错误的原因是由于我们控制层组件类中的onSubmit方法为protected类型。protectedModelAndViewonSubmit(ObjectformBean)throwsException{}因此,为了能够对它进行测试,我们需要将它从protected类型改变为public类型publicModelAndViewonSubmit(ObjectformBean)throwsException{}5、编程它:packagespringwebappttest;importjunit.framework.*;importspringwebapp.*;importorg.springframework.web.servlet.*;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.FileSystemXmlApplicationContext;publicclassTestUserLoginControllerextendsTestCase{privateUserLoginControlleruserLoginController=null;privateUserLoginFormuserLoginForm;protectedvoidsetUp()throwsException{super.setUp();//下面根据实际改变,同时还应该注意“userLoginController”的定义是在那个*.xml文件中/*StringspringXMLFilePath=E:/SpringWebApp+UserLogin/SpringWeb/WEB-INF/springapp-servlet.xml;*/StringspringXMLFilePath=D:/JUnitExmp/SpringWebApp+UserLogin/SpringMVCWeb/WEB-INF/userManager.xml;ApplicationContextapplicationContext=newFileSystemXmlApplicationContext(springXMLFilePath);userLoginController=(UserLoginController)applicationContext.getBean(userLoginController);userLoginForm=newUserLoginForm();}protectedvoidtearDown()throwsException{userLoginController=null;userLoginForm=null;super.tearDown();}publicvoidtestOnSubmit()throwsException//测试成功登录的状态{userLoginForm.setUserName(yang);//产生业务参数userLoginForm.setUserPassword(1234);ModelAndViewmodelAndView=userLoginController.onSubmit(userLoginForm);StringexpectedTargetPage=userLoginController.getLoginSuccess();StringactualTargetPage=modelAndView.getViewName();//获得实际跳转目标assertEquals(expectedTargetPage,actualTargetPage);}publicvoidtestLoginInCorrect()throwsException//测试登录失败的状态{userLoginForm.setUserName(yang);userLoginForm.setUserPassword(123456);ModelAndViewmodelAndView=userLoginController.onSubmit(userLoginForm);StringexpectedTargetPage=userLoginController.getLoginFailure();assertEquals(expectedTargetPage,modelAndView.getViewName());//获得实际跳转目标userLoginForm.setUserName(zhang);userLoginForm.setUserPassword(1234);modelAndView=userLoginController.onSubmit(userLoginForm);assertEquals(expectedTargetPage,modelAndView.getViewName());//获得实际跳转目标}}6、执行本测试用例将产生出下面的结果状态同时可以观察输出的信息7、学员练习:针对我们上面的Web项目中的业务层组件UserLoginImple类进行测试。(1)引入UserLoginImple.java到我们的测试项目(2)在针对UserLoginImple类提供一个新的测试用例----JUnit(3)编程该测试用例----但要采用IoC容器来获得被测试类的对象(4)执行该测试用例对基于Spring框架的J2EEMVC组件测试---—涉及有HttpServletRequest等容器的对象时则采用Springmock技术
本文标题:对基于Spring框架的J2EEMVCWeb组件测试
链接地址:https://www.777doc.com/doc-2536027 .html