title: Spring Web Flow: setting request parameter for JUnit test
body: <h2>Introduction</h2>
  
  <p>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:</p>
  
  <pre><code>@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);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <h2>The problem</h2>
  
  <p>In the class to test I read the contents of a request parameter from the web flow like this:</p>
  
  <pre><code>RequestContext requestContext = RequestContextHolder.getRequestContext();
  ExternalContext externalContext = requestContext.getExternalContext();
  String printPressed = externalContext.getRequestParameterMap().get("printPressed");
  </code></pre>
  
  <p>I would need to set the "printPressed" request parameter in my JUnit test method like this:</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>That is, however impossible because the map is immutable and results in an UnsupportedOperationException.</p>
  
  <p>The question is: how to set a request parameter to the requestParameterMap in the class <a href="http://static.springsource.org/spring-webflow/docs/2.0.9.RELEASE/javadoc-api/org/springframework/webflow/context/ExternalContext.html" rel="nofollow">org.springframework.webflow.context.ExternalContext</a>? The parameter map returned by <a href="http://static.springsource.org/spring-webflow/docs/2.0.9.RELEASE/javadoc-api/org/springframework/webflow/context/ExternalContext.html#getRequestParameterMap%28%29" rel="nofollow">getRequestParameterMap</a> is immutable, so I cannot add the parameter there. I noticed that there is e.g. an object <a href="http://static.springsource.org/spring-webflow/docs/2.0.9.RELEASE/javadoc-api/org/springframework/webflow/test/MockParameterMap.html" rel="nofollow">MockParameterMap</a>, which sounds promising: <em>"A extension of parameter map that allows for mutation of parameters. Useful as a stub for testing."</em></p>
  
  <p><strong>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?</strong></p>