How do I unit test a subprocess user task?


I modeled a simple process to find out how call activities work and I have trouble unit testing the subtask.

So basically my sample.bpmn is start => choose-action (user task) => subprocess-task (call activity) =>after-subprocess (user task) => last-task (user task) => end with a call to
subprocess.bpmn which is start => anytask (service|usertask) => end

subprocess-task has a DelegateVariableMapping that logs and sets variable anytext. If I use a service task in the subprocess, my unit test runs fine, if I use a simple user task, it does not as task() is null. So I suppose I have to retrieve the subprocess instance task - how can I do that?


import static org.camunda.bpm.engine.test.assertions.ProcessEngineAssertions.*;
import static org.camunda.bpm.engine.test.assertions.ProcessEngineTests.*;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.*;
import org.camunda.bpm.extension.process_test_coverage.junit.rules.TestCoverageProcessEngineRuleBuilder;
import org.junit.*;

public class SampleProcessTest {

private ProcessInstance processInstance;

public static ProcessEngineRule rule = TestCoverageProcessEngineRuleBuilder.create().build();

public void before() {


	processInstance = processEngine().getRuntimeService()


@Deployment(resources = { "sample.bpmn", "subprocess.bpmn" })
public void shouldPassSubprocess() {

	// choose "action"
	complete(task(), withVariables("action", "subprocess",
			"anytext", "set by unit test"));

	// subprocess-task
	ProcessInstance subProcessInstance = processEngine().getRuntimeService().createProcessInstanceQuery().processDefinitionKey("subprocess").singleResult();

	if ("demo".equals(task().getAssignee())) {

	// after-subprocess
	complete(task(), withVariables("anytext", "I ran through after-subprocess"));

	// choose-"action"
	complete(task(), withVariables("action", null));

	// last-task
	assertThat(processInstance).hasPassed("choose-"action"", "subprocess-task", "after-subprocess", "last-task").isEnded();



Hi Janina,

Could you maybe share your entire test case on github? That makes it easier to understand the situation.


I edited the source code above and while deleting unneccesary information, I found out I set one wrong constant value for the subprocess definition key :blush:
So now the source code above works. But I have still one question: I get the subprocess like this

ProcessInstance subProcessInstance = processEngine().getRuntimeService().createProcessInstanceQuery().processDefinitionKey("subprocess").singleResult();

which means I just search for it knowing its name and there can only be one instance running during my test case.
Is it possible to get it via the running main process instance? I suppose there must be any reference from an instance to its sub-instances.

Check out this query filter:

1 Like