使用Spring mock测试

SpringMVC的Web程序可以使用Spring mock进行测试,不用每一次都需要部署到容器里边,mock会帮你模拟Web环境。

部署SpringMVC的Web程序,我们需要在web.xml里面设置listener来创建我们的ApplicationContext,在mock里面,我们也需要创建ApplicationContext。

测试类需要继承AbstractDependencyInjectionSpringContextTests,并覆盖protected String[] getConfigLocations()方法,这个方法就是告诉mock你的applicationContext配置文件和-servlet.xml文件在哪个地方。例子:

    @Override
    protected String[] getConfigLocations() {
        return new String[]{"applicationContext-*.xml","file:D:\\MyApp\\test\\web\\WEB-INF\\dispatcher-servlet.xml"};
    }

这里注意,默认是在类路径里面找。我的applicationContext配置文件放在了类路径,所以不需要加其他的路径,否则,使用物理路径。

之后我做一个简单的测试。我现在有一个LoginController.java,用来判断登陆的userid和userpwd是否正确,正确则去到success.jsp,否则去到fail.jsp。假设现在有一个账号aa,密码是bb。

我要先在ApplicationContext里面拿控制器的实例出来:

LoginController loginController = (LoginController) this.applicationContext.getBean("loginController");

这个applicationContext通过覆盖的方法getConfigLocations创建的。

然后我们使用MockHttpServletRequest类和MockHttpServletResponse类模拟HttpServletRequest和HttpServletResponse:

        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

再设置request的参数:

request.setMethod("POST");

request.addParameter("userid","aa");

request.addParameter("userpwd","bb");

最后调用loginController的login方法(我这里使用MutiActionController):

ModelAndView mav = loginController.login(request,response);

完整的测试代码如下:

public class TestLoginController extends AbstractDependencyInjectionSpringContextTests {

    @Override
    protected String[] getConfigLocations() {
        return new String[]{"applicationContext-*.xml","file:D:\\MyApp\\test\\web\\WEB-INF\\dispatcher-servlet.xml"};
    }
    
    @SuppressWarnings("static-access")
    public void testLogin() throws MalformedURLException, IOException{
        LoginController loginController = (LoginController) this.applicationContext.getBean ("loginController");
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        request.setMethod("POST");

        request.addParameter("userid","aa");

         request.addParameter("userpwd","bb");
         ModelAndView mav = loginController.login(request,response);

        assertEquals("success",mav.getViewName());
    }     
}

Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework.mock.web.DelegatingServletInputStream.class org.springframework.mock.web.DelegatingServletOutputStream.class org.springframework.mock.web.HeaderValueHolder.class org.springframework.mock.web.MockExpressionEvaluator.class org.springframework.mock.web.MockFilterChain.class org.springframework.mock.web.MockFilterConfig.class org.springframework.mock.web.MockHttpServletRequest.class org.springframework.mock.web.MockHttpServletResponse.class org.springframework.mock.web.MockHttpSession.class org.springframework.mock.web.MockMultipartFile.class org.springframework.mock.web.MockMultipartHttpServletRequest.class org.springframework.mock.web.MockPageContext.class org.springframework.mock.web.MockRequestDispatcher.class org.springframework.mock.web.MockServletConfig.class org.springframework.mock.web.MockServletContext.class org.springframework.mock.web.PassThroughFilterChain.class org.springframework.mock.web.portlet.MockActionRequest.class org.springframework.mock.web.portlet.MockActionResponse.class org.springframework.mock.web.portlet.MockMultipartActionRequest.class org.springframework.mock.web.portlet.MockPortalContext.class org.springframework.mock.web.portlet.MockPortletConfig.class org.springframework.mock.web.portlet.MockPortletContext.class org.springframework.mock.web.portlet.MockPortletPreferences.class org.springframework.mock.web.portlet.MockPortletRequest.class org.springframework.mock.web.portlet.MockPortletRequestDispatcher.class org.springframework.mock.web.portlet.MockPortletResponse.class org.springframework.mock.web.portlet.MockPortletSession.class org.springframework.mock.web.portlet.MockPortletURL.class org.springframework.mock.web.portlet.MockRenderRequest.class org.springframework.mock.web.portlet.MockRenderResponse.class org.springframework.test.AbstractDependencyInjectionSpringContextTests.class org.springframework.test.AbstractSingleSpringContextTests.class org.springframework.test.AbstractSpringContextTests.class org.springframework.test.AbstractTransactionalDataSourceSpringContextTests.class org.springframework.test.AbstractTransactionalSpringContextTests.class org.springframework.test.AssertThrows.class org.springframework.test.ConditionalTestCase.class org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.class org.springframework.test.annotation.DirtiesContext.class org.springframework.test.annotation.ExpectedException.class org.springframework.test.annotation.IfProfileValue.class org.springframework.test.annotation.NotTransactional.class org.springframework.test.annotation.ProfileValueSource.class org.springframework.test.annotation.Repeat.class org.springframework.test.annotation.SystemProfileValueSource.class org.springframework.test.annotation.Timed.class org.springframework.test.jpa.AbstractAspectjJpaTests.class org.springframework.test.jpa.AbstractJpaTests.class org.springframework.test.jpa.OrmXmlOverridingShadowingClassLoader.class org.springframework.test.web.AbstractModelAndViewTests.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值