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



我能够运行一个Spring Boot应用程序与Camunda工作流管理。我的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文件。我的应用程序。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替换硬编码的JDBC URL。Url}根本不起作用。尽管我已经设置了camunda。bpm。generic-properties。properties。generalResourceWhitelistPattern,应用程序测试。如果是Yml,很明显它没有被读取。

基本上有两种方法来测试camunda spring引导应用程序:

1:在内存中运行camunda引擎,不使用弹簧。这对于测试流程流、委托行为等非常有用。在这种情况下,您将使用"processenginerule";从核心(junit4风格)或junit5扩展(camunda-bpm-junit5)。您可能会模拟很多服务和回购,因为您关注的是流量。您不需要使用"activeprofiles"进行注释,因为您没有运行spring上下文。

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

这两种方法都不是"更好"的,它们关注的是不同的事情。根据测试金字塔,你会有很多"真实的"测试。单元测试,甚至不运行camunda在内存中只是为了验证你的代码工作,一些基于规则的流测试和一些集成测试使用spring引导测试。

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

注:当你混合使用纯单元和spring集成测试时,你应该切换到基于构造函数的注入,这样在不使用反射的情况下更容易手动创建服务的实例。

最新更新