[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;

Hi, Ingo

I dont seem to be able to test my dmn file with the @ExtendWith(ProcessEngineExtension.class) functionality. It works fine for bpmn files.

What is the correct way to test input/output of dmn tables?

Br

Frank

Hi @FrankReneSorensen,

this is a running example from my latest training:

@ExtendWith(ProcessEngineCoverageExtension.class)
public class ProcessJUnitTest {
  
  @Test
  @Deployment(resources = "tweetApproval.dmn")
  public void testTweetApprovalIBM() {
    Map<String, Object> variables = withVariables(
        "content", "ibm process server is slow",
        "email", "don't care");
    
    DmnDecisionTableResult tableResult = decisionService().evaluateDecisionTableByKey("tweetApproval", variables);
    
    assertThat(tableResult.getFirstResult()).contains(entry("approved", false));
  }
}

Hope this helps, Ingo

Do you have the training available? I would very much like to attend, if it is possible?

Br

Frank

Hi @FrankReneSorensen,

this test is just a small piece of our Camunda Platform for Java Developer training: Camunda Platform Training for Java Developers - Camunda.

If you use Camunda Platform for a longer time, you may see some pieces you are already familiar with.

Hope this helps, Ingo

Hi guys,

Just to get your feedback on the following point:

In JUnit4, the DMN Engine dependency comes with a JUnit Rule, tests in JUnit4 use the DmnEngineRule for bootstrapping the DMN engine, and then get the DMN engine with

@Rule
  public DmnEngineRule dmnEngineRule = new DmnEngineRule();

  @Test
  public void test() {
    DmnEngine dmnEngine = dmnEngineRule.getDmnEngine();

As demonstrated in Testing Decisions with the DMN Engine | docs.camunda.org
All in one single artifact: camunda-engine-dmn.

In JUnit5, the Rule is obsolete, as @Rule is replaced by @ExtendWith. The new annotation bootstraps the engine, same principle so far, then to get the DMN we have to ask the decisionService to evaluate the table as:

DmnDecisionTableResult tableResult = decisionService().evaluateDecisionTableByKey("tweetApproval", variables);

The decision service is only available in the artifact: camunda-engine-7.X.jar, so one more artifact.

Meaning, I have to include the whole process engine artifact in order to access the decision service in my tests, even if I do not want to access the other services.

Is there a way to test the DMN Engine with only the DMN engine artifact in JUnit5, as it was possible in JUnit4 ?

Any input is greatly appreciated.

Cheers