Spring Web Flow - 如何使用conversationScope中已有的值设置单元测试?(Spring Web Flow - How can I set up unit test with values already in conversationScope?)

我正在使用Spring Web Flow 2.0开发一个项目。

我试图对以决策状态开始的流进行单元测试。 决策状态检查conversationScope上的对象的值。 我无法弄清楚如何将值插入conversationScope进行单元测试。

我努力了:

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

但是,当我调用startFlow(context)时,似乎清除了该值。

我也尝试过:

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

但是测试失败并且错误地告诉我,我无法从决策状态恢复,只能从视图状态恢复。

有谁知道如何在conversationScope上插入模拟值,以便我可以测试这些情况?

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?

最满意答案

这不是很明显,但我想出了这个:

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 ,并将其中的一部分复制并粘贴到单元测试中。

这是测试网络流程。

<?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>

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>

更多推荐

conversationScope,context,测试,电脑培训,计算机培训,IT培训"/> <meta name="