[Junit5] ProcessEngineRule compatibility with Junit5

Hi Everyone,

Does anyone have tested camunda processes with JUnit5?

I’m getting NullPointerException when i access runtimeService from ProcessEngineRule.

@Rule
@ClassRule 
ProcessEngineRule processEngineRule = TestCoverageProcessEngineRuleBuilder.create().build();

runtimeService = processEngineRule.getRuntimeService();

ProcessInstance processInstance =runtimeService.startProcessInstanceByKey("OneActivityProcess"); //throws NPE

Or should I’ve fallback to Junit4 ?

Hi @aravindhrs,

@Rule is JUnit4 only. Have a look at this extension to write process tests with JUnit5 and @ExtendWith(ProcessEngineExtension.class): GitHub - camunda-community-hub/camunda-bpm-junit5: Camunda Community Extension to write process tests with JUnit5

Hope this helps, Ingo

1 Like

@Ingo_Richtsmeier Thanks :+1:

Can we change the below config StandaloneInMemProcessEngineConfiguration to ProcessCoverageInMemProcessEngineConfiguration?

With JUnit5 I hope it has support for ProcessCoverageInMemProcessEngineConfiguration. What do you think?

<bean id="processEngineConfiguration" class="org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<bean id="processEngineConfiguration"
          class="org.camunda.bpm.extension.process_test_coverage.junit.rules.ProcessCoverageInMemProcessEngineConfiguration">

Hi @aravindhrs,

process test coverage and JUnit 5 is still an open issue: Feature Request > Add support for JUnit 5 · Issue #45 · camunda-community-hub/camunda-bpm-process-test-coverage · GitHub.

The latest refactoring from Simon should make it easier to get it done.

Currently I’m trying to offer greater flexibility to provide a preconfigured engine to the JUnit 5 extension: camunda-bpm-junit5/core/src/test/java/org/camunda/bpm/extension/junit5/registerExtension at use_own_process_engine · camunda-community-hub/camunda-bpm-junit5 · GitHub

But it is still work in progress as the build fails.

Hope this helps, Ingo

1 Like

@Ingo_Richtsmeier How to mock the Process Engine services in the facade api over camunda java api?

For example,

	@Autowired
	private HistoryService historyService;

	@Override
	public HistoricProcessInstanceDto getHistoricProcessInstance(String businessKey, String processDefinitionKey) {
		log.info("Querying historic process instance for businessKey: {}", businessKey);

		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
				.processInstanceBusinessKey(businessKey).processDefinitionKey(processDefinitionKey)
				.singleResult();

         if (Objects.nonNull(historicProcessInstance)) {
			HistoricProcessInstanceDto historicProcessInstanceDto = HistoricProcessInstanceDto
					.fromHistoricProcessInstance(historicProcessInstance);
			return historicProcessInstanceDto
		}

		return null;
	}

@TestInstance(Lifecycle.PER_CLASS)
@ExtendWith(MockitoExtension.class)
class ProcessInstanceHistoryServiceTest {

    @Mock
	HistoryService historyService;

	@InjectMocks
	ProcessInstanceHistoryService processInstanceHistoryService = new ProcessInstanceHistoryServiceImpl();

	@Test
	void testGetHistoricProcessInstance() {
        when(historyService.createHistoricProcessInstanceQuery().processDefinitionKey(Mockito.anyString())
				.processInstanceBusinessKey(Mockito.anyString())).thenReturn(historicProcessInstance);

		HistoricProcessInstanceDto historicProcessInstanceDto = processInstanceHistoryService.getHistoricProcessInstance("Test1",
				"OneActivityProcess");
		//assertions based on historicProcessInstanceDto
	}

It throws NPE. Have you tried mocking process engine services like above scenario?

Note that here i’m not testing the bpmn process, instead i’m testing the wrapper java method which has process engine services.

Solved with the below snippets:

@ExtendWith(MockitoExtension.class)

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private HistoryService historyService;