Enabling and Testing User Tasks
Goal
In this lab you will update your process to utilize a User Task to review the tweet. And then you’ll utilize the Camunda Platform Assert APIs and Task APIs in your JUnit test to test the process under more realistic conditions.
Short description
- Change the manual tasks to user tasks.
- Add the tasks to the
management
group. - Extend the JUnit test to query for the user task and complete it.
- Assert the expected state of the process instance and the tasks.
Detailed steps
Go back into Modeler and using the wrench icon in the Context Pad change the Review tweet task from Manual to User Task. And now when the process is run you’ll need to add code to the JUnit test to complete the task.
Set the Candidate groups of the Review tweet task to
management
. This will be important later during testing when using APIs to retrieve tasks by Candidate groups.Go back to the JUnit test you developed in the last lab. If you review the code you’ll realize this test will fail since the only assertion made in it is the one where is the process has ended. With the updated process we know that once the process is started it will be waiting at the Review tweet task. How would we use
assertThat
to make sure the process is waiting at Review tweet? For more hints regarding this assertion have a look here in the Camunda documentation. Put this assertion after the start of the process instance. You can use the helperfindId("Task Name")
to get the (generated)activityId
of a task from the modeler.Next, we need to query for available tasks for
management
Candidate groups. This is accomplished using the taskService object. Add the following code after the assertion you added in Step 3. Be sure to addjava.util.List
to your imports. You’ll notice we’re using a method to retrieve only tasks for themanagement
Candidate group for our particular process instance. We should only retrieve one item.List<Task> taskList = taskService() .createTaskQuery() .taskCandidateGroup("management") .processInstanceId(processInstance.getId()) .list();
For more information regarding what options are available with
createTaskQuery()
have a look hereOnce we have the ‘list’ of tasks we should make sure the list is not null and that it only has one item. Use
org.assertj.core.api.Assertions
library (available viaimport static org.assertj.core.api.Assertions.*;
, it may be removed by Organize Imports) to make the following assertions:assertThat(taskList).isNotNull(); assertThat(taskList).hasSize(1);
Now we need to complete the task and to make it more realistic we’ll complete the task with the ‘approved’ boolean instead of setting it at the beginning of the process. First, remove the line that adds the ‘approved’ boolean to the Map ie.
variables.put("approved", true);
that starts off the process.Next, get the one and only task at position zero.
Task task = taskList.get(0);
Complete this task using taskService() and passing in the approved variable. To complete the task, we need the ID of the task which can be obtained from the task object.
Map<String, Object> approvedMap = new HashMap<String, Object>(); approvedMap.put("approved", true); taskService().complete(task.getId(), approvedMap);
For more information on
taskService
see Task Service Interface Java DocsMake sure the process ended assertion completes the JUnit test.
Run the testHappyPath test case and check for the green bar. Be sure you’re not attempting to tweet a duplicate tweet that will result in an error.
Optional
Try the helpers from the camunda-bpm-assert library to write a shorter test and test more facts from the process instance and the task.