Integration Tests in Camunda Spring Boot App

Hello Everyone!
I have a problem with writing integration tests in Spring Boot application with Camunda Spring boot Starter.

I have a test, which is testing rest endpoint, the logic of this endpoint also use Process Engine.

Next block of code had NullPointerException:

1. RuntimeService runtimeService = processEngine.getRuntimeService();
2. ProcessInstance pi = runtimeService.createProcessInstanceQuery()
3.             .processInstanceBusinessKey(String.valueOf(requestId)).singleResult();
4. Execution execution = runtimeService.createExecutionQuery().processInstanceId(pi.getId()).activityId(CONFIRM_REQUEST_CREATION).singleResult();

In this case pi variable is NULL. So I have NPE.

My test is looks like:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class RequestControllerIT extends BaseIT {
 
 private String REQUEST_CONTROLLER_URL = "my/path/to/endpoint";     

 @Test
 public void testMyEndpoint() {
     MockHttpServletRequestBuilder request = put(REQUEST_CONTROLLER_URL)
               .accept(APPLICATION_JSON_VALUE);
     mockMvc.perform(request)
            .andExpect(MockMvcResultMatchers.status().isOk());
 }

Any ideas?

Try deploying the process as part of the Unit Test definition.

For example, given a file called “myprocess.bpmn” under test/resources/workflow/ folder you should specify the test similar to this:

@Test
@Deployment(resources = { "workflow/myprocess.bpmn" })
public void testGetDecisionRequirementDefinition() {

    rule.getProcessEngine()....

}

Ensure that @Deployment import is from package “org.camunda.bpm.engine.test”, if necessary add the relevant entry to your pom.xml.

This pattern doesn’t require the full Spring Boot application to be started, and uses an in-memory H2 DB so is much, much quicker.

UPDATE: Also add an entry such as this to the unit test class, that way you can access the Camunda API directly vie the “rule” variable:

@Rule
public ProcessEngineRule rule = new StandaloneInMemoryTestConfiguration().rule();