Using ZeebeClient in PostConstruct get error during tests "ZeebeClient is currently not initialized. Maybe you run outside of a testcase?"

Hello! I’m newby in Camunda.
I try to declare a new worker inside a PostConstructor.
It’s fine when I execute the application but my tests fail with the error:

ZeebeClient is currently not initialized. Maybe you run outside of a testcase?

I’m using spring-boot 3.2.2 and Camunda 8.6.7

<dependency>
  <groupId>io.camunda</groupId>
  <artifactId>spring-boot-starter-camunda-sdk</artifactId>
  <version>${camunda.version}</version>
</dependency>
<dependency>
  <groupId>io.camunda</groupId>
  <artifactId>spring-boot-starter-camunda-tasklist</artifactId>
  <version>${camunda.version}</version>
</dependency>
<dependency>
  <groupId>io.camunda</groupId>
  <artifactId>spring-boot-starter-camunda-test-testcontainer</artifactId>
  <version>${camunda.version}</version>
  <scope>test</scope>
</dependency>

Does anyone have any idea why?
Thank you very much
Elisa

Hi @crocie, welcome to the forums! Can you share your test that is failing, along with the relevant setup code?

Hello @nathan.loding sure!
My test is this:

@SpringBootTest
@ZeebeSpringTest
@Deployment(resources = "classpath:workflow/SimpleServiceTask.bpmn")
@Slf4j
class ProcessSimpleServiceTaskTest extends PSQLContainerTestSuite {

  private static final String BPMN_ID = "Process_Simple_Service_Task";

  @Autowired
  private ZeebeClient client;

  @SpyBean
  private SimpleServiceWorker simpleServiceWorker;

  @Test
  void testProcessSimpleServiceTask() {

    ProcessInstanceEvent processInstance = client.newCreateInstanceCommand()
      .bpmnProcessId(BPMN_ID)
      .latestVersion()
      .send()
      .join();

    assertThat(processInstance).isNotNull();
    assertThat(processInstance.getProcessInstanceKey()).isPositive();

    waitForProcessInstanceCompleted(processInstance.getProcessInstanceKey(), Duration.ofSeconds(10));

    Mockito.verify(simpleServiceWorker).executeWorkerJob(any(JobClient.class), any(ActivatedJob.class));

    assertThat(processInstance)
      .hasPassedElement("EndEvent_Process_Simple_Service_Task")
      .isCompleted();
  }
}

And My dummy simple service task is this:


@Component
@Slf4j
public class SimpleServiceWorker {

  private final ZeebeClient zeebeClient;

  public SimpleServiceWorker(ZeebeClient zeebeClient) {
    this.zeebeClient = zeebeClient;
  }

  @PostConstruct
  public void registryWorker() {
    zeebeClient
      .newWorker()
      .jobType("showcase.simple_service")
      .handler(new JobHandler() {
        @Override
        public void handle(JobClient client, ActivatedJob job) throws Exception {
          log.info("Execution of {}",job.getType().toLowerCase());
          Map<String, Object> map = new HashMap<>();
          map.put("var1", "Test set variable from job handler");
          client.newCompleteCommand(job.getKey()).variables(map).send().join();
        }
      })
      .tenantIds(List.of("tenant_test"))
      .timeout(Duration.ofSeconds(10))
      .open();

    log.debug("REGISTERED SERVICE");
  }
}

Hi @crocie ,

you could use an @EventListener for the ZeebeClientCreatedEvent and register your worker there instead of injecting the zeebe client to your bean.

This is a better fit for the zeebe client lifecycle as the zeebe client is reset whenever the engine is reset in a test.

Best,

Jonathan

1 Like

Hello @jonathan.lukas thanks a lot, it works perfectly!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.