How do I unit test a subprocess user task?

Hi,

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?

Regards,
Janina

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;

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

@Before
public void before() {

	init(rule.getProcessEngine());

	processInstance = processEngine().getRuntimeService()
			.startProcessInstanceByKey("sample");

	assertThat(processInstance).isActive();
}

@Test
@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();
	assertThat(subProcessInstance).isActive();
	assertThat(task()).isNotNull();

	if ("demo".equals(task().getAssignee())) {
		complete(task());
	}
	assertThat(processInstance).hasVariables("anytext");

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

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

	// last-task
	complete(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.

Thanks,
Thorben

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: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.8/org/camunda/bpm/engine/runtime/ProcessInstanceQuery.html#superProcessInstanceId(java.lang.String)

1 Like