无法在测试上下文中注入 Spring 状态机没有 JpaStateMachineRepository 类型的合格 bean

问题描述

我正在编写一个测试用例来注入我的 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...