Spring Webflow测试过渡与评估标签

时间:2018-06-19 14:54:18

标签: junit spring-webflow

在单元测试中测试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”,而不是预期的“ end”。

这是我的春季网络流程的第二版:

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

我将“ evaluate”标记从“ communicateCredentials”视图状态转换为“ on entry”标记从“ end”视图状态转换,并且单元测试正在工作...

如果我想在“ 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>

我发现解决方案更优雅,并且单元测试也可行。

谢谢您的建议。

1 个答案:

答案 0 :(得分:0)

我认为问题是<evaluate><transition>中的工作方式。如果未返回“成功”结果,则不进行转换,而是返回到当前的<view-state>。尽管文档中不清楚,但我认为您的空字符串不是成功案例。

尽管有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的源代码,看来唯一被认为成功的字符串值是“成功”,“是”和“真”:

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