问题描述
我正在编写一个测试用例来注入我的 Spring 状态机的一个实例并输入一些事件,但到目前为止还没有让它正确启动上下文
状态机配置
@Configuration
@EnableStateMachineFactory(contextEvents = false)
@requiredArgsConstructor
@Slf4j
public class StateMachineConfig
extends EnumStateMachineConfigurerAdapter<ExecutionStates,ExecutionEvents> {
private final AbstractStateMachineMonitor stateMachineMonitor;
private final StateMachineRuntimePersister<Events,States,String> stateMachineRuntimePersister;
@Override
public void configure(final StateMachineConfigurationConfigurer<Events,States> config)
throws
Exception {
config.withConfiguration()
.listener(new StateMachineListenerAdapter<>());
config.withMonitoring()
.monitor(stateMachineMonitor);
config.withPersistence()
.runtimePersister(stateMachineRuntimePersister);
super.configure(config);
}
@Override
public void configure(final StateMachinestateConfigurer<Events,States> states)
throws
Exception {
states.withStates().initial(ExecutionStates.INITED,initialAction()).end(ExecutionStates.COMPLETED)
.states(EnumSet.allOf(ExecutionStates.class));
}
@Override
public void configure(final StateMachineTransitionConfigurer<Events,States> transitions)
throws
Exception {
transitions
.withExternal()
.source(ExecutionStates.INITED).target(ExecutionStates.COMPLETED)
.event(ExecutionEvents.COMPLETE);
}
持久化配置
@Configuration
public class StateMachinePersistenceConfiguration {
@Bean
public StateMachineRuntimePersister<Events,String> stateMachineRuntimePersister(
final JpaStateMachineRepository jpaStateMachineRepository) {
return new JpaPersistingStateMachineInterceptor<>(jpaStateMachineRepository);
}
}
未通过测试
@RunWith(springrunner.class)
@SpringBoottest(classes = { StateMachineConfig.class,StateMachineMonitor.class,StateMachinePersistenceConfiguration.class})
@ContextConfiguration(classes = {StateMachinePersistenceConfiguration.class})
class Test {
@Autowired
private StateMachine<Events,States> machine;
@Test
public void testinitialState() throws Exception {
StateMachineTestPlan<Events,States> plan =
StateMachineTestPlanBuilder.<Events,States>builder()
.defaultAwaitTime(2)
.stateMachine(machine1)
.step()
.expectStateChanged(1)
.expectStateEntered(Events.INIT)
.and()
.build();
plan.test();
}
依赖
- Java 11
- spring boot 2.4.2 spring-boot-starter-data-jpa 2.4.2
- spring-statemachine-autoconfigure、starter-data-jpa、spring-statemachine-starter 和 spring-statemachine-test 2.4.0
- junit 5
任何帮助将不胜感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)