3.4. 单元测试
由于Flowable DMN可嵌入Java所以写引擎DMN定义的单元测试就像编写常规单元测试一样简单。
Flowable支持JUnit单元测试版本4和5。
在JUnit 需要在5种风格中使用 org.flowable.dmn.engine.test.FlowableDmnTest 注解
或手动注册 org.flowable.dmn.engine.test.FlowableDmnExtension 。
FlowableDmnTest 注释只是一个元注释,注册了 FlowableDmnExtension
(即 @ExtendWith(FlowableDmnExtension.class)).
这将使DmnEngine服务可用作测试和生命周期方法的参数
(@BeforeAll, @BeforeEach, @AfterEach, @AfterAll).
在每次测试前,默认情况下使用类路径 flowable.dmn.cfg.xml 资源初始化dmnEngine。
需要使用不同的配置文件来指定org.flowable.dmn.engine.test.DmnConfigurationResource 注释(见第二个例子)。
配置资源相同时,Dmn在多个单元测试中,发动机将静态缓存。
通过使用 FlowableDmnExtension ,您可以使用测试方法 org.flowable.dmn.engine.test.DmnDeployment 注解或 org.flowable.dmn.engine.test.DmnDeploymentAnnotation 注解。
若同时使用 @DmnDeployment 和 @DmnDeploymentAnnotatio 那么 @DmnDeployment 优先,@DmnDeploymentAnnotation 将被忽略。
用于测试方法 @DmnDeployment 每次测试前都会部署注释 DmnDeployment#resources 中定义的dmn文件。
如果没有定义资源,则为 testClassName.testMethod.dmn 正式的资源文件将部署在与测试类相同的包中。
在测试结束时,部署将被删除,包括所有相关的dmn定义、执行等。
有关更多信息,请参阅 DmnDeployment 类。
考虑到这一切,JUnit 5测试看起来如下:
使用默认资源JUnit 5测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@FlowableDmnTest
class MyDecisionTableTest {
@Test
@DmnDeploymentAnnotation
void simpleDmnTest(DmnEngine dmnEngine) {
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map executionResult = ruleService.createExecuteDecisionBuilder()
.decisionKey("extensionUsage")
.variable("inputVariable1", 2)
.variable("inputVariable2", "test2")
.executeWithSingleResult();
Assertions.assertThat(executionResult).containsEntry("output1", "test1");
}
}
使用JUnit 5.你也可以部署ID(带有org.flowable.dmn.engine.test.DmnDeploymentId_)注入测试和生命周期方法。
使用自定义资源JUnit 5测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18@FlowableDmnTest
@DmnConfigurationResource("flowable.custom.dmn.cfg.xml")
class MyDecisionTableTest {
@Test
@DmnDeploymentAnnotation
void simpleDmnTest(DmnEngine dmnEngine) {
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map executionResult = ruleService.createExecuteDecisionBuilder()
.decisionKey("extensionUsage")
.variable("inputVariable1", 2)
.variable("inputVariable2", "test2")
.executeWithSingleResult();
Assertions.assertThat(executionResult).containsEntry("output1", "test1");
}
}
在编写JUnit 可使用4单元测试 org.flowable.dmn.engine.test.FlowableDmnRule 规则。通过这一规则,DMN可通过发动机和服务getter获得。包含此 Rule 将启用注解 org.flowable.dmn.engine.test.DmnDeploymentAnnotation (请参阅以上内容,了解其使用和配置) 默认配置文件将在类路径中找到。 在使用相同的资源配置时,DMN在多个单元测试中,发动机可以静态缓存。
自定义引擎配置也可以为规则提供。
以下代码片段显示了使用JUnit 4测试样式和 FlowableDmnRule 用法示例(并传输可选自定义配置):
JUnit 4 test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public class MyDecisionTableTest {
@Rule
public FlowableDmnRule flowableDmnRule = new FlowableDmnRule("custom1.flowable.dmn.cfg.xml");
@Test
@DmnDeploymentAnnotation
public void ruleUsageExample() {
DmnEngine dmnEngine = flowableDmnRule.getDmnEngine();
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map executionResult = ruleService.createExecuteDecisionBuilder()
.decisionKey("extensionUsage")
.variable("inputVariable1", 2)
.variable("inputVariable2", "test2")
.executeWithSingleResult();
Assertions.assertThat(executionResult).containsEntry("output1", "test1");
}
}