Camunda 共享引擎 Spring Junit 工厂 bean 'processEngine' 返回空值

Camunda Shared Engine Spring Junit factory-bean 'processEngine' returned null

我正在使用 Spring MVC 和带有共享流程引擎的 Camunda 编写一个简单的流程应用程序。现在我想添加简单的测试用例,我 运行 进入流程引擎 returns 每个工厂方法都为空的问题。当 运行 应用程序流程引擎 returns 按预期提供服务时。

使用共享流程引擎时,我应该如何为 JUnit 测试配置 camunda?

这是我的 camunda 流程引擎配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- bind the process engine service as Spring Bean -->
<bean name="processEngineService" class="org.camunda.bpm.BpmPlatform" factory-method="getProcessEngineService" />

<!-- bind the default process engine as Spring Bean -->
<bean name="processEngine" factory-bean="processEngineService" factory-method="getDefaultProcessEngine" />

<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
<bean id="authorizationService" factory-bean="processEngine" factory-method="getAuthorizationService"/>

<!-- bootstrap the process application -->
<bean id="processApplication" class="org.camunda.bpm.engine.spring.application.SpringServletProcessApplication" />
</beans>

这是我简单测试的相关部分class:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
        "file:src/main/webapp/WEB-INF/process-conf.xml",
        "file:src/main/webapp/WEB-INF/hibernate-conf.xml",
        "file:src/main/webapp/WEB-INF/camunda-conf.xml",
        "file:src/main/webapp/WEB-INF/dispatcher-servlet.xml",
})
public class OrderControllerTest {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    @Before
    public void setup() {
        DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
        this.mockMvc = builder.build();
    }

    @Test
    public void testTest() throws Exception {
        ResultMatcher ok = MockMvcResultMatchers.status().isOk();
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/");
        this.mockMvc.perform(builder)
                .andExpect(ok);
    }
}

这是启动junit测试时抛出的异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryService' defined in URL [file:src/main/webapp/WEB-INF/camunda-conf.xml]: factory-bean 'processEngine' (or a BeanPostProcessor involved) returned null

我通过基于猜测的解决方法解决了问题: 似乎测试是 运行d 直接在 java 中,没有 tomcat 服务器实例。因此,没有可以访问的共享流程引擎。

解决方法: 我为嵌入式流程引擎创建了另一个 camunda 配置,它使用与 tomcat 实例中配置的共享引擎相同的数据库。然后可以实例化流程引擎,测试 运行ning 正常。我的猜测是,应该谨慎不要同时进行 tomcat 和测试 运行。

这是嵌入式流程引擎配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/CamundaProcessEngine_001?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"/>
        <property name="username" value="####"/>
        <property name="password" value="####"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="processEngineConfiguration" class="org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="jobExecutorActivate" value="false" />
        <!-- turn off metrics reporter -->
        <property name="dbMetricsReporterActivate" value="false" />
        <property name="history" value="full" />
    </bean>

    <bean id="processEngine" class="org.camunda.bpm.engine.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>

    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
    <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
    <bean id="authorizationService" factory-bean="processEngine" factory-method="getAuthorizationService"/>

    <bean id="activitiRule" class="org.camunda.bpm.engine.test.ProcessEngineRule">
        <property name="processEngine" ref="processEngine" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>