【问题标题】:Spring webflow testing transition with evaluate tag带有评估标签的 Spring Webflow 测试转换
【发布时间】:2018-06-28 20:33:57
【问题描述】:

我在单元测试中测试我的 spring webflow 执行时遇到了困难。

我正在使用 spring webflow 2.4.5.RELEASE。

这是我在测试中遇到困难的第一个版本的 spring webflow:

<view-state id="communicateCredentials" view="grant-phr-access/communicate-credentials">
    <transition on="next" to="end">
        <evaluate expression="grantPHRAccessServiceImpl.grantPHRAccess(grantPHRAccessForm)" result="flowScope.personAccountCredentials"/>
    </transition>
</view-state>

<view-state id="end" view="grant-phr-access/end">
    <transition on="finish" to="redirectToSpecialistHome"/>
</view-state>

这是我的单元测试:

@Test
public void transitionFromCommunicateCredentialsToEndTest() {
    // Configure data for this test
    setCurrentState("communicateCredentials");
    getFlowScope().put("grantPHRAccessForm", new GrantPHRAccessForm());
    when(grantPHRAccessService.grantPHRAccess(any(GrantPHRAccessForm.class))).thenReturn("");

    // Trigger flow event and check the result
    assertCurrentStateEquals("communicateCredentials");
    context.setEventId("next");
    resumeFlow(context);
    verify(grantPHRAccessService, times(1)).grantPHRAccess(any(GrantPHRAccessForm.class));
    assertCurrentStateEquals("end");
}

这是错误信息:

junit.framework.ComparisonFailure: The current state 'communicateCredentials' does not equal the expected state 'end' 
Expected :end
Actual   :communicateCredentials

带有“验证”的模拟测试有效,这意味着过渡已被评估。

我还在调试模式下检查了“flowScope.personAccountCredentials”的内容,并且我有预期的空字符串(由模拟的 grantPHRAccessService.grantPHRAccess() 返回)。

但状态始终是“communicateCredentials”,而不是预期的“结束”。

这是我的spring web flow的第二个版本:

<view-state id="communicateCredentials" view="grant-phr-access/communicate-credentials">
    <transition on="next" to="end"/>
</view-state>

<view-state id="end" view="grant-phr-access/end">
    <!-- evaluate has been moved from "communicateCredentials" view-state transition to here -->
    <on-entry>
        <evaluate expression="grantPHRAccessServiceImpl.grantPHRAccess(grantPHRAccessForm)" result="flowScope.personAccountCredentials"/>
    </on-entry>
    <transition on="finish" to="redirectToSpecialistHome"/>
</view-state>

我从“end”视图状态的“on entry”标签中的“communicateCredentials”视图状态转换中移动了“evaluate”标签,并且我的单元测试正在运行...

如果我想在“communicateCredentials”视图状态转换中保留“评估”,我是否在单元测试中遗漏了什么?

感谢您的帮助:)。

@更新

这是我的解决方案:

<view-state id="communicateCredentials" view="grant-phr-access/communicate-credentials">
    <transition on="next" to="grantPHRAccess"/>
</view-state>

<!-- Extract the evaluate into proper action-state -->
<action-state id="grantPHRAccess">
    <evaluate expression="grantPHRAccessServiceImpl.grantPHRAccess(grantPHRAccessForm)" result="flowScope.personAccountCredentials"/>
    <transition to="end"/>
</action-state>

<view-state id="end" view="grant-phr-access/end">
    <transition on="finish" to="redirectToSpecialistHome"/>
</view-state>

我发现解决方案更优雅,并且单元测试有效。

感谢您的建议。

【问题讨论】:

    标签: junit spring-webflow


    【解决方案1】:

    我认为问题在于 &lt;evaluate&gt;&lt;transition&gt; 中的工作方式。如果它没有返回“成功”结果,则不会进行转换,而是返回到当前的&lt;view-state&gt;。虽然文档中并不清楚,但我认为您的空字符串不是成功案例。

    尽管the documentation still seems to indicate that only false would be considered non-successI've seen some evidence that's not the case.

    查看org.springframework.webflow.engine.support.ActionTransitionCriteria 的源代码,似乎唯一被视为成功的字符串值是“success”、“yes”和“true”:

    /**
     * The result event id that should map to a <code>true</code> return value.
     */
    private String[] trueEventIds = new String[] { "success", "yes", "true" };
    

    【讨论】:

    • 的结果保存在 flowScope 中。我不会在转换中使用我的服务结果。
    • 在我看来,您是在告诉您的模拟对象返回“”,这将导致 WebFlow 返回到当前状态。您需要让它返回“成功”或“是”或“真”以使其进入下一个状态。而且,当然,您的操作方法同样需要返回其中一个值。或者也许 void 也会起作用,我不确定。但是,是的,您可以在动作状态中而不是在转换中执行动作,这样您就可以绕过这个问题。
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2019-01-03
    相关资源
    最近更新 更多