сamunda替换测试中外部任务的行为

holgip5t  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(468)

我创建了简单的camundaspringboot项目,还使用switcher创建了简单的bpmn过程(5.5 kb)
我使用带有外部实现的服务任务作为springbean。我想为进程编写测试,但不想测试bean是如何工作的。因为一般来说,我使用外部实现连接到db,并将参数保存到上下文或rest调用到内部应用程序。例如,我想跳过执行服务任务(一个),而是为切换器设置变量。我尝试将camundabpmAssert场景用于测试过程,并编写了简单的测试工作流测试。
我注意到如果我对one.class使用@mockbean,那么camunda将跳过委托执行。如果使用@mock,那么camunda将执行委托执行。
对不起,英语不好

@服务公共类一实现javadelegate{private final random=new random();

  1. @Override
  2. public void execute(DelegateExecution execution) throws Exception {
  3. System.out.println("Hello, One!");
  4. execution.setVariable("check", isValue());
  5. }
  6. public boolean isValue() {
  7. return random.nextBoolean();
  8. }

}
工作流测试
@springboottest@runwith(springrunner.class)@deployment(resources=“process.bpmn”)公共类workflowtest扩展了abstractprocessengineruletest{

  1. @Mock
  2. private ProcessScenario insuranceApplication;
  3. @MockBean
  4. private One one;
  5. @Before
  6. public void init() {
  7. MockitoAnnotations.initMocks(this);
  8. Mocks.register("one", one);
  9. }
  10. @Test
  11. public void shouldExecuteHappyPath() throws Exception {
  12. // given
  13. when(insuranceApplication.waitsAtServiceTask("Task_generator")).thenReturn(externalTaskDelegate -> {
  14. externalTaskDelegate.complete(withVariables("check", true));
  15. }
  16. );
  17. String processDefinitionKey = "camunda-test-process";
  18. Scenario scenario = Scenario.run(insuranceApplication)
  19. .startByKey(processDefinitionKey) // either just start process by key ...
  20. .execute();
  21. verify(insuranceApplication).hasFinished("end_true");
  22. verify(insuranceApplication, never()).hasStarted("three");
  23. verify(insuranceApplication, atLeastOnce()).hasStarted("two");
  24. assertThat(scenario.instance(insuranceApplication)).variables().containsEntry("check", true);
  25. }

}

pvabu6sv

pvabu6sv1#

我找到了两个解决方案:
有点小毛病。如果测试中委托的user@mockbean。委托将被跳过,但您在处理进程引擎变量时遇到问题。
使用一个限定符创建两个bean,并使用概要文件进行测试和生产。我曾经为本地启动设置默认配置文件,为测试设置测试配置文件。

相关问题