Spring Web Flow-如何使用sessionScope中已经存在的值设置单元测试? [英] Spring Web Flow - How can I set up unit test with values already in conversationScope?
问题描述
我正在使用Spring Web Flow 2.0开发一个项目.
I am working on a project using Spring Web Flow 2.0.
我正在尝试对以决策状态开始的流进行单元测试.决策状态检查conversationScope
上的对象的值.我无法弄清楚如何在单元测试的conversationScope
中插入一个值.
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.
我尝试过:
getConversationScope().put("someName", value);
MockExternalContext context = new MockExternalContext();
startFlow(context);
但是,当我调用startFlow(context)
时,似乎已清除该值.
However, it seems that when I call startFlow(context)
the value is cleared.
我也尝试过:
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.
有人知道我如何在conversationScope
上插入模拟值,以便我可以测试这些情况吗?
Does anyone know how I can insert mock values on the conversationScope
so that I may test these cases?
推荐答案
这不是很明显,但是我想到了:
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");
}
我必须深入研究底层的AbstractXmlFlowExecutionTests.startFlow()
,以查看其如何实例化FlowExecution
,然后将其中的一些内容复制并粘贴到单元测试中.
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.
这是测试网络流程.
<?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>
这篇关于Spring Web Flow-如何使用sessionScope中已经存在的值设置单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!