Spring State Machine use StateMachineTestPlan to test a withChoice transition

650 Views Asked by At

If I have a StateMachine set up as below:

transitionConfigurer
  .withExternal()
  .source(FIRST)
    .event(EVENT_1)
    .target(SECOND)

  .and()
  .withChoice()
  .source(SECOND)
    .first(THIRD, new Guard(someService))
    .last(FIRST)

  .and()
  .withExternal()
  .source(THIRD)
    .event(EVENT_2)
    .target(FIRST);

How can I test the withChoice using a StateMachineTestPlan.

I have a StateMachineTestPlan set up as below, this creates a State Machine with an initial state of SECOND, I want to then be able to test the choice. However, when I perform the .step() in the test plan nothing happens:

createStateMachineAndSetInitialState(SECOND, workflowCreator);

StateMachineTestPlan<State, Event> plan = StateMachineTestPlanBuilder.<State, Event> builder()
  .defaultAwaitTime(1)
  .stateMachine(machine)
  .step()
    .expectState(THIRD)
  .and()
  .build();

  plan.test();
1

There are 1 best solutions below

0
On

First of all, can you please share your custom override for the method below?

You need to make sure you have configured choice states like this:

@Override
  public void configure(final StateMachineStateConfigurer<States, Events> states)
      throws Exception {
    states
        .withStates()
        .initial(States.FIRST)
        .choice(States.SECOND) 
        .end(States.LAST)
        .states(States.enumSet);
  }

It seems like you reached state (SECOND) but did not enter in the CHOICE pseudo-state. In which state have your test stopped at?