Reputation: 21
I have difficulties with testing my spring webflow execution in a unit test.
I'm using spring webflow 2.4.5.RELEASE.
Here is the first version of my the spring webflow with I'm having difficulties to test:
<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>
Here is my unit test:
@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");
}
Here is the error message:
junit.framework.ComparisonFailure: The current state 'communicateCredentials' does not equal the expected state 'end'
Expected :end
Actual :communicateCredentials
The mockito test with "verify" works, that means that the transition was evaluated.
I also checked in debug mode the content of the "flowScope.personAccountCredentials" and I have the empty String as expected (returned by the mocked grantPHRAccessService.grantPHRAccess()).
But the state is always "communicateCredentials" and not "end" as expected.
Here is the second version of my 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>
I moved the "evaluate" tag from the "communicateCredentials" view-state transition in the "on entry" tag from the "end" view-state and my unit test is working...
If I want to keep the "evaluate" in the "communicateCredentials" view-state transition, am I missing something in my unit test?
Thank you for your help :).
@Update
Here is my solution:
<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>
I find the solution more elegant and the unit test works.
Thank you for your suggestions.
Upvotes: 1
Views: 862
Reputation: 5105
I believe the issue is the way <evaluate>
works in a <transition>
. If it doesn't return a "success" result, it doesn't take the transition, but instead goes back to the current <view-state>
. And while it's unclear in the documentation, I think your empty string is not a success case.
Despite the fact that the documentation still seems to indicate that only false
would be considered non-success, I've seen some evidence that's not the case.
Looking at the source code for org.springframework.webflow.engine.support.ActionTransitionCriteria
, it appears the only string values considered success are "success", "yes", and "true":
/**
* The result event id that should map to a <code>true</code> return value.
*/
private String[] trueEventIds = new String[] { "success", "yes", "true" };
Upvotes: 0