Spring Web Flow:为JUnit测试设置请求参数

时间:2012-05-15 12:33:52

标签: java spring mocking junit4 spring-webflow-2

简介

我有一个Java / Tomcat / Spring 2.5 / Spring Webflow 2.0.9 Web应用程序。为了测试我创建了JUnit测试的一些页面,它们将值设置为表单对象并模仿来自浏览器的请求。 JUnit测试类看起来像这样:

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@ContextConfiguration(locations = { "classpath:/WebTests-context.xml", "classpath:/messageSourceConfig.xml" })
@RunWith(SpringJUnit4ClassRunner.class)

public class InitechValidatorTests {

    protected MockHttpSession session;
    protected MockHttpServletRequest request;

    //test methods omitted

    protected void startSession() {
        session = new MockHttpSession();
    }

    protected void endSession() {
        session.clearAttributes();
        session = null;
    }

    protected void startRequest() {
        request = new MockHttpServletRequest();
        request.setSession(session);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

        MockRequestContext mrc = new MockRequestContext();
        org.springframework.webflow.execution.RequestContextHolder.setRequestContext(mrc);

        MessageContext messageContext = org.springframework.webflow.execution.RequestContextHolder.getRequestContext()
                .getMessageContext();
        this.validationContext.setMessageContext(messageContext);
    }

    protected void endRequest() {
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).requestCompleted();
        RequestContextHolder.resetRequestAttributes();
        request = null;

        org.springframework.webflow.execution.RequestContextHolder.setRequestContext(null);
    }
}

对于每个测试方法,我调用startRequest和endRequest方法(在第一个测试方法之前调用startSession,在最后一个测试方法之后调用endSession)。如果没有这种结构,我无法使用例如我正在测试的代码中的RequestContextHolder。

问题

在要测试的类中,我从Web流中读取请求参数的内容,如下所示:

RequestContext requestContext = RequestContextHolder.getRequestContext();
ExternalContext externalContext = requestContext.getExternalContext();
String printPressed = externalContext.getRequestParameterMap().get("printPressed");

我需要在我的JUnit测试方法中设置“printPressed”请求参数,如下所示:

org.springframework.webflow.execution.RequestContext requestContext = org.springframework.webflow.execution.RequestContextHolder
                    .getRequestContext();
ExternalContext externalContext = requestContext.getExternalContext();
Map requestParamMap = externalContext.getRequestParameterMap().asMap();
requestParamMap.put("printPressed", "true");

然而,这是不可能的,因为地图是不可变的并导致UnsupportedOperationException。

问题是:如何在类org.springframework.webflow.context.ExternalContext中为requestParameterMap设置请求参数? getRequestParameterMap返回的参数映射是不可变的,因此我无法在那里添加参数。我注意到有例如一个对象MockParameterMap,听起来很有希望:“参数映射的扩展,允许参数变异。可用作测试的存根。”

如何将MockParameterMap注入测试类中的ExternalContext?或者是否有其他方法来设置单元测试的请求参数?

1 个答案:

答案 0 :(得分:4)

通过修改startRequest方法解决了这个问题,以便将MockParameterMap设置为新的MockExternalContext对象,然后将其设置为MockRequestContext

MockParameterMap mpp = new MockParameterMap();
MockExternalContext mec = new MockExternalContext(mpp);
mrc.setExternalContext(mec);

然后在JUnit测试方法中,我可以随后设置参数:

org.springframework.webflow.execution.RequestContext requestContext = org.springframework.webflow.execution.RequestContextHolder
                    .getRequestContext();
ExternalContext externalContext = requestContext.getExternalContext();
MockParameterMap requestParameterMap = (MockParameterMap) externalContext.getRequestParameterMap();
requestParameterMap.put("printPressed", "true");