Skip to content

Spring webflow testing transition with evaluate tag #45

@mchavaillaz

Description

@mchavaillaz

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>

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 find more logical to keep the "evaluate" in the "communicateCredentials" view-state transition, am I missing something in my unit test?

Thank you for your support :).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions