Flowable 7.1.0 Spring Boot测试报错:找不到flowable.cfg.xml
搭建了基于Flowable 7.1.0、Groovy、Spring Boot和Gradle的项目,手动运行一切正常。但将测试流程文件复制到test/resources/processes目录下,编写了如下JUnit 5测试类并添加@FlowableTest注解后,运行测试报错找不到flowable.cfg.xml。
测试类代码
@FlowableTest //@SpringBootTest class ProcessTest { private ProcessEngine processEngine private RuntimeService runtimeService private TaskService taskService @BeforeEach void setUp(ProcessEngine processEngine) { this.processEngine = processEngine this.runtimeService = processEngine.getRuntimeService() this.taskService = processEngine.getTaskService() } @Test @Deployment (resources = [ "processes/one-task-process-test.bpmn20.xml" ]) void testOneTaskProcess() { runtimeService.startProcessInstanceByKey("oneTaskProcessTest") Task task = taskService.createTaskQuery().singleResult() assertEquals("my task", task.getName()) taskService.complete(task.getId()) assertEquals(0, runtimeService.createProcessInstanceQuery().count()) } }
Gradle依赖配置
dependencies { implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '3.4.0' implementation group: 'org.flowable', name: 'flowable-spring-boot-starter-process', version: '7.1.0' implementation group: 'org.flowable', name: 'flowable-spring-boot-starter-rest-api', version: '7.0.0.M1' //implementation group: 'org.flowable', name: 'flowable-idm-spring', version: '7.1.0' //include groovy script engine for groovy script tasks implementation group: 'org.apache.groovy', name: 'groovy-jsr223', version: '4.0.24' implementation group: 'com.h2database', name: 'h2', version: '2.3.232' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '3.4.0' //for actuator bean //implementation group: 'org.springframework.security', name: 'spring-security-web', version: '6.4.1' //implementation group: 'org.springframework.security', name: 'spring-security-config', version: '6.4.1' implementation 'org.apache.groovy:groovy:4.0.14' testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.junit.jupiter:junit-jupiter-engine' testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '3.4.0' testImplementation group: 'com.h2database', name: 'h2', version: '2.3.232' }
错误日志
... class path resource [flowable.cfg.xml] cannot be opened because it does not exist java.io.FileNotFoundException: class path resource [flowable.cfg.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:215) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:319) at org.flowable.common.engine.impl.cfg.BeansConfigurationHelper.parseEngineConfiguration(BeansConfigurationHelper.java:38) at org.flowable.common.engine.impl.cfg.BeansConfigurationHelper.parseEngineConfigurationFromResource(BeansConfigurationHelper.java:62) at org.flowable.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:176) at org.flowable.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:172) at org.flowable.engine.impl.test.TestHelper.getProcessEngine(TestHelper.java:254) at org.flowable.engine.test.FlowableExtension.createProcessEngine(FlowableExtension.java:198) at org.flowable.engine.test.FlowableExtension.lambda$getTestHelper$4(FlowableExtension.java:194) ...
核心原因
@FlowableTest是针对非Spring Boot环境的测试注解,它默认会从类路径加载flowable.cfg.xml来初始化ProcessEngine。但你的项目是Spring Boot集成Flowable,应该依托Spring Boot的自动配置机制,而不是传统的XML配置文件。
解决步骤
切换测试注解
去掉@FlowableTest,启用注释掉的@SpringBootTest。这个注解会启动Spring Boot测试上下文,自动加载Flowable的自动配置,无需手动指定flowable.cfg.xml。简化服务注入方式
在Spring Boot测试中,可以直接通过@Autowired注入Flowable的核心服务Bean,不需要手动从ProcessEngine获取。修改后的测试类如下:@SpringBootTest class ProcessTest { @Autowired private RuntimeService runtimeService @Autowired private TaskService taskService @Test @Deployment(resources = ["processes/one-task-process-test.bpmn20.xml"]) void testOneTaskProcess() { runtimeService.startProcessInstanceByKey("oneTaskProcessTest") Task task = taskService.createTaskQuery().singleResult() assertEquals("my task", task.getName()) taskService.complete(task.getId()) assertEquals(0, runtimeService.createProcessInstanceQuery().count()) } }统一Flowable依赖版本
注意到你依赖中的flowable-spring-boot-starter-rest-api使用的是7.0.0.M1,和其他Flowable依赖的7.1.0版本不一致,这可能引发兼容性问题。建议统一升级到7.1.0:implementation group: 'org.flowable', name: 'flowable-spring-boot-starter-rest-api', version: '7.1.0'
关于flowable.cfg.xml的说明
这个文件是Flowable在非Spring Boot环境下的核心配置文件,用于定义数据源、事务管理器、引擎参数等,通常放在src/main/resources或src/test/resources目录下。但在Spring Boot集成Flowable的项目中,Flowable会自动读取application.properties或application.yml中的配置来初始化引擎,完全不需要这个XML文件。
内容的提问来源于stack exchange,提问作者WILLIAM WOODMAN

