编程语言
首页 > 编程语言> > Java-Spring Web Flow:设置JUnit测试的请求参数

Java-Spring Web Flow:设置JUnit测试的请求参数

作者:互联网

介绍

我有一个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.

问题

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

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?还是有其他方法可以设置用于单元测试的请求参数?

解决方法:

通过修改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");

标签:mocking,junit4,spring-webflow-2,spring,java
来源: https://codeday.me/bug/20191101/1982794.html