Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Web Flow: setting request parameter for JUnit test

Introduction

I have a Java/Tomcat/Spring 2.5/Spring Webflow 2.0.9 web application. To test some of the pages I have created JUnit tests, which set values to the form objects and mimic the requests coming from the browser. The JUnit test class looks something like this:

@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);
    }
}

For each test method I call the startRequest and endRequest methods (startSession is called before the first test method and endSession after the last test method). Without this construct I couldn't use e.g. RequestContextHolder in the code I'm testing.

The problem

In the class to test I read the contents of a request parameter from the web flow like this:

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

I would need to set the "printPressed" request parameter in my JUnit test method like this:

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");

That is, however impossible because the map is immutable and results in an UnsupportedOperationException.

The question is: how to set a request parameter to the requestParameterMap in the class org.springframework.webflow.context.ExternalContext? The parameter map returned by getRequestParameterMap is immutable, so I cannot add the parameter there. I noticed that there is e.g. an object MockParameterMap, which sounds promising: "A extension of parameter map that allows for mutation of parameters. Useful as a stub for testing."

How can I inject the MockParameterMap to the ExternalContext in the test class? Or is there some other way to set the request parameters for unit testing?

like image 978
simon Avatar asked Dec 02 '25 14:12

simon


1 Answers

Solved the problem by modifying the startRequest method so that I set the MockParameterMap to a new MockExternalContext object, which is set to the MockRequestContext followingly:

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

Then in the JUnit test method I can set the parameter followingly:

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");
like image 97
simon Avatar answered Dec 04 '25 04:12

simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!