December 1, 2008

Spring WebFow Testing - How to add objects to the session

While spring has a good framework for testing webflows, we found out that you need to add objects to the session which the webflow action expect.

Some may think that this is a bad practive but we had a need and that is how we coded it.

To add items to the session do the follwoing

  1. Create a Test Base Class BaseFlowTest which extends AbstractXmlFlowExecutionTests
  2. override the following methods 
protected ViewSelection startFlow(MutableAttributeMap input) {
  ExternalContext context = createExternalContext(null);
  context.getSessionMap().put("myObjectName",  new SessionObject());
  return startFlow(input, context);
protected ViewSelection signalEvent(String eventId, ParameterMap requestParameters) throws FlowExecutionException {
   ExternalContext context = createExternalContext(requestParameters);
   context.getSessionMap().put("myObjectName",  new SessionObject());
   return signalEvent(eventId, context);

Test Code

public void testStartFlow() {
   ViewSelection o = startFlow(setAttributes());
   ApplicationView view = applicationView(o);
   assertViewNameEquals(getLoginPageView(), view);