Introduction
I have a Java / Tomcat / Spring 2.5 / Spring Webflow 2.0.9 web application. To test some of the pages, I created JUnit tests that set values ββfor form objects and simulate requests coming from the browser. The JUnit 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;
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 design, I could not use, for example. RequestContextHolder in the code I'm testing.
Problem
In the class for validation, I read the contents of the request parameter from the web stream as follows:
RequestContext requestContext = RequestContextHolder.getRequestContext();
ExternalContext externalContext = requestContext.getExternalContext();
String printPressed = externalContext.getRequestParameterMap().get("printPressed");
"printPressed" JUnit :
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.
: requestParameterMap org.springframework.webflow.context.ExternalContext? , getRequestParameterMap, , . , , , MockParameterMap, : " , . ".
MockParameterMap ExternalContext ? ?