【发布时间】: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