5

I am working on a project using Spring Web Flow 2.0.

I am trying to unit test a flow that begins with a decision state. The decision state checks the value of an object that is on the conversationScope. I cannot figure out how to insert a value into the conversationScope for the unit test.

I have tried:

getConversationScope().put("someName", value);
MockExternalContext context = new MockExternalContext();
startFlow(context);

However, it seems that when I call startFlow(context) the value is cleared.

I also tried:

MockExternalContext context = new MockExternalContext();
setCurrentState("someDecisionState");
resumeFlow(context)

But the test fails with an error telling me that I cannot resume from a decision state, only from a view state.

Does anyone know how I can insert mock values on the conversationScope so that I may test these cases?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
TM.
  • 108,298
  • 33
  • 122
  • 127

1 Answers1

6

It's not obvious, but I came up with this:

public void testFoo() {
    FlowExecution flowExecution = getFlowExecutionFactory().createFlowExecution(getFlowDefinition());
    updateFlowExecution(flowExecution);
    flowExecution.getConversationScope().put("fooBar", "goo");
    flowExecution.start(null, new MockExternalContext());        
    assertCurrentStateEquals("fooView");
}

I had to dig into the underlying AbstractXmlFlowExecutionTests.startFlow() to see how it was instantiating the FlowExecution, and copy and paste some of that into the unit test.

Here's the test web flow.

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <action-state id="decideFoo">
        <evaluate expression="conversationScope.fooBar" />
        <transition on="goo" to="fooView" />
        <transition on="gar" to="barView" />
    </action-state>

    <view-state id="fooView" />

    <view-state id="barView" />

</flow>
Scott Bale
  • 10,649
  • 5
  • 33
  • 36
  • Nice, will try when I get a chance – TM. Aug 24 '09 at 23:15
  • Works perfectly, thanks! Seems like the key bit is using flowExecution.start(null, context), as using startFlow(context) seems to clear out anything you had set up on your flowExecution. – TM. Aug 28 '09 at 18:52
  • Glad to have helped. Yes, the startFlow(context) method creates a new FlowExecution behind the scenes. With this example you can use your own FlowExecution, at the expense of writing more code to manage it somewhat manually. – Scott Bale Aug 28 '09 at 20:34