Map<String, Object> variables = new HashMap<String, Object>();
variables.put("initiator", "kermit");
RuntimeService runtimeService = activitiRule.getRuntimeService();
String procId = runtimeService.startProcessInstanceByKey("reviewSaledLead").getId();
TaskService taskService = activitiRule.getTaskService();
Task task = taskService.createTaskQuery().taskAssignee("kermit").singleResult();
assertEquals("Provide new sales lead", task.getName());
// After completing the task, the review subprocess will be active
taskService.complete(task.getId());
Task ratingTask = taskService.createTaskQuery().taskCandidateGroup("accountancy").singleResult();
assertEquals("Review customer rating", ratingTask.getName());
Task profitabilityTask = taskService.createTaskQuery().taskCandidateGroup("management").singleResult();
assertEquals("Review profitability", profitabilityTask.getName());
// Complete the management task by stating that not enough info was provided
// This should throw the error event, which closes the subprocess
variables = new HashMap<String, Object>();
variables.put("notEnoughInformation", true);
taskService.complete(profitabilityTask.getId(), variables);
// The 'provide additional details' task should now be active
Task provideDetailsTask = taskService.createTaskQuery().taskAssignee("kermit").singleResult();
assertEquals("Provide additional details", provideDetailsTask.getName());
// Providing more details (ie. completing the task), will activate the subprocess again
taskService.complete(provideDetailsTask.getId());
List<Task> reviewTasks = taskService.createTaskQuery().orderByTaskName().asc().list();
assertEquals("Review customer rating", reviewTasks.get(0).getName());
assertEquals("Review profitability", reviewTasks.get(1).getName());
// Completing both tasks normally ends the process