繁体   English   中英

如何在 Spring Boot 中设置 Camunda 进行单元测试

[英]How to set up Camunda for unit testing in Spring Boot

我能够使用 Camunda 运行 Spring Boot 应用程序以进行工作流管理。 我的 pom.xml 与 Camunda 相关的依赖项如下所示。

<dependencyManagement>
        ...
        <dependency>
            <groupId>org.camunda.bpm</groupId>
            <artifactId>camunda-bom</artifactId>
            <version>7.15.0</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

    <dependency>
        <groupId>org.camunda.bpm.springboot</groupId>
        <artifactId>camunda-bpm-spring-boot-starter</artifactId>
        <version>7.15.0</version>
    </dependency>

    <!-- REST API -->
    <dependency>
        <groupId>org.camunda.bpm.springboot</groupId>
        <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
        <version>7.15.0</version>
    </dependency>

    <!-- dashboard -->
    <dependency>
        <groupId>org.camunda.bpm.springboot</groupId>
        <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
        <version>7.15.0</version>
    </dependency>

    <dependency>
        <groupId>org.camunda.bpm</groupId>
        <artifactId>camunda-engine-plugin-spin</artifactId>
    </dependency>

    <dependency>
        <groupId>org.camunda.spin</groupId>
        <artifactId>camunda-spin-dataformat-all</artifactId>
    </dependency>

我只有一个.BPMN 文件。 我的 application.yml 文件如下所示:

spring.main.banner-mode: console
server.port: 9090

springdoc:
  model-and-view-allowed: true
  swagger-ui:
    operationsSorter: alpha

spring.jpa:
  hibernate:
    ddl-auto: none
  show-sql: true
  properties:
    hibernate:
      format_sql: true
spring.datasource:
  initialization-mode: always
  platform: postgres
  url: jdbc:postgresql://xxxx
  username: xxxx
  password: xxxx

spring.flyway.enabled: false

camunda.bpm:
  admin-user:
    id: demo
    password: demo
  generic-properties:
    properties:
      generalResourceWhitelistPattern: "[a-zA-Z0-9,'_\\$\\-\\.\\+\\!\\*\\(\\)]+|camunda-admin"

在代码中,我所要做的只是声明 Camunda 对象,我可以在没有进一步设置的情况下使用它们:

@AutoWired
protected final RuntimeService runtimeService;

@AutoWired
protected final TaskService taskService;

@AutoWired
protected final IdentityService identityService;

现在我正在尝试编写单元测试。 似乎有不止一种方法可以设置 Camunda 进行单元测试,但我无法获得我发现有效的任何示例。 通过反复试验,我设法获得了以下代码来设置 Camunda 进行单元测试。

@ActiveProfiles("test")
public class EntitlementServiceTest {

    private RuntimeService runtimeService;
    private TaskService taskService;
    private IdentityService identityService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
            .setJdbcUrl("jdbc:h2:mem:database_user;DB_CLOSE_ON_EXIT=FALSE")
            .setJobExecutorActivate(true)
            .buildProcessEngine();
        runtimeService = processEngine.getRuntimeService();
        taskService = processEngine.getTaskService();
        identityService = processEngine.getIdentityService();
    }

    ...
}

但我不确定这是否是正确的做法,所以如果有人指出这一点会更好。 这些代码似乎没有从用于单元测试的应用程序文件 application-test.yml 加载任何内容。 用 ${spring.datasource.url} 替换硬编码的 JDBC URL 根本不起作用。 即使我已经设置了 camunda.bpm.generic-properties.properties.generalResourceWhitelistPattern,application-test.yml,很明显它没有被读取。

您基本上有两种方法来测试 camunda spring 启动应用程序:

第 1:在 memory 中运行 camunda 引擎,而不是 spring。这对于测试流程、委托行为等非常有用。 在这种情况下,您将使用核心(junit4 样式)或 junit5 扩展(camunda-bpm-junit5)中的“ProcessEngineRule”。 你可能会模拟很多服务和回购,因为你专注于 camunda 流程。 您不需要使用“ActiveProfiles”进行注释,因为您没有运行 spring 上下文。

第二:使用 SpringRunner 或 SprinExtension 运行测试,并让 spring 处理引擎设置。 这只是一个简单的 spring 启动测试,它将运行您通过 yml 和配置文件指定的所有内容,并且您将能够注入所有 camunda 服务。

这两种方法都不是“更好”的,它们专注于不同的事情。 根据测试金字塔,您将有许多“真正的”单元测试,甚至不会在 memory 中运行 camunda 只是为了验证您的代码是否正常工作,一些基于规则的流程测试和一些使用 spring 启动测试的集成测试。

camunda 最佳实践指南将帮助配置场景:https://camunda.com/best-practices/testing-process-definitions/

旁注:当您混合普通单元和 spring 集成测试时,您应该切换到基于构造函数的注入,以便在不使用反射的情况下更轻松地手动创建服务实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM