Hello. I created simple Camunda spring boot project and also created simple BPMN process with switcher. process.bpmn (5.5 KB)
I used service task with external implementation as a spring beans. I want to write tests for process but I don’t want to test how beans works. Because in general I use external implementation for connection to DB and save parameter to context or REST call to internal apps. For example I want skip execute service task(one) and instead set variables for switcher. I tried to use camunda-bpm-assert-scenario for test process and wrote simple test WorkflowTest.
I noticed if I use @MockBean for One.class then Camunda skip delegate execution. If use @Mock then Camunda execute delegate execution.
PS Sorry for bad english
One
@Service
public class One implements JavaDelegate {
private final Random random = new Random();
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("Hello, One!");
execution.setVariable("check", isValue());
}
public boolean isValue() {
return random.nextBoolean();
}
}
WorkflowTest
@SpringBootTest
@RunWith(SpringRunner.class)
@Deployment(resources = "process.bpmn")
public class WorkflowTest extends AbstractProcessEngineRuleTest {
@Mock
private ProcessScenario insuranceApplication;
@MockBean
private One one;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
Mocks.register("one", one);
}
@Test
public void shouldExecuteHappyPath() throws Exception {
// given
when(insuranceApplication.waitsAtServiceTask("Task_generator")).thenReturn(externalTaskDelegate -> {
externalTaskDelegate.complete(withVariables("check", true));
}
);
String processDefinitionKey = "camunda-test-process";
Scenario scenario = Scenario.run(insuranceApplication)
.startByKey(processDefinitionKey) // either just start process by key ...
.execute();
verify(insuranceApplication).hasFinished("end_true");
verify(insuranceApplication, never()).hasStarted("three");
verify(insuranceApplication, atLeastOnce()).hasStarted("two");
assertThat(scenario.instance(insuranceApplication)).variables().containsEntry("check", true);
}
}