Hi,
I am currently involved in the testing of processes with an embedded zeebe engine, using spring-zeebe-test. Testing the happy path using the original job workers is doing well, but I get problems when I try to mock a Job Worker in order to test some edge cases.
The original worker (a Spring Bean) follows this structure (simplified):
@Component
public class MyWorker {
    @ZeebeWorker(type = "myWorker")
    public void doSomething(final JobClient client, final ActivatedJob job) {
        if (specialCondition) {
            throw new ZeebeBpmnError("myErrorCode","Failure!");
        }
        // ...some custom code goes here...
        client.newCompleteCommand(job.getKey())
                .send()
                .whenComplete((result, exception) -> {
                    if (exception != null) {
                        log.warn("Job completion failed: {}", exception.getMessage());
                    } else {
                        log.info("Job completion succeeded");
                    }
                });
    }
My test class looks like this (simplified as well):
@SpringBootTest
@ZeebeSpringTest
@ExtendWith(MockitoExtension.class)
public class MyTestClass {
    @Autowired
    private ZeebeClient zeebeClient;
    @Autowired
    private ZeebeTestEngine zeebeTestEngine;
    @MockBean
    private MyWorker worker;
    @Test
    void testEdgeCase() {
        ProcessInstanceEvent processInstance = zeebeClient.newCreateInstanceCommand()
                .bpmnProcessId(BPMN_PROCESS_ID)
                .latestVersion()
                .send().join();
         doThrow(new ZeebeBpmnError("myErrorCode","Failure!")).when(worker).doSomething(any(),any());
        
        waitForProcessInstanceCompleted(processInstance);
        BpmnAssert.assertThat(... checking several assertions ...);
    }
}
I am getting an error:
org.awaitility.core.ConditionTimeoutException: Assertion condition defined as a io.camunda.zeebe.spring.test.ZeebeTestThreadSupport Process with key [...] was not completed within 5 seconds.
I am using Java 17, SpringBoot 2.7.1 and JUnit 5.
For now I have two questions:
- What am I doing wrong here? Is mocking the worker not the right approach?
- In the code shown above I’m using the @MockBean-Annotation in order to use the mocked worker instead of the original one. This is inconvenient if I want to use the original worker in other test methods of the same test class. It would be better if I could register the original worker or an appropriate mock with the zeebe test engine in each test method, depending on which case I want to test. How can I do that?
Thanks in advance for any help!
Jens