Spring Web Flow - 如何使用对话范围中已有的值设置单元测试?

发布于 2024-07-18 04:37:41 字数 675 浏览 6 评论 0原文

我正在开发一个使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2024-07-25 04:37:41

这并不明显,但我想到了这一点:

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文