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

  1. 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.

  2. 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.

  3. 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 helper findId("Task Name") to get the (generated) activityId of a task from the modeler.

  4. 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 add java.util.List to your imports. You’ll notice we’re using a method to retrieve only tasks for the management 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 here

  5. Once 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 via import 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);
    
  6. 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.

  7. Next, get the one and only task at position zero.

       Task task = taskList.get(0);
    
  8. 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 Docs

  9. Make sure the process ended assertion completes the JUnit test.

  10. 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.

On this Page: