Complete the Test Coverage with Process Instance Modification

Goal

In this lab you’ll add the JUnit methods to complete the test coverage. Use camunda-bpm-assert-shortcuts and process instance modifications for this.

Short description

  • Add a new JUnit test method with the process deployed.
  • Start the process directly before the unit you want to test.
  • Assert that the unit has the expected result.
  • Inspect the test coverage.

Detailed steps

  1. Add an new method to your JUnit test. Name it testTweetRejected. Be sure to include the @Deployment annotation that’s part of testHappyPath in order to deploy the process into the process engine when you run the new test. Add the @Test annotation.

  2. Create a process instance and have it startAfterActivity of Review tweet. Your code for the process instance should look something like this:

        ProcessInstance processInstance = runtimeService()
            .createProcessInstanceByKey("TwitterQAProcess")
            .setVariables(varMap)
            .startAfterActivity(findId("Review tweet"))
            .execute();
    

    For more information regarding process instance modification see here. From this code you can see that you’re starting a process instance with variables and moving the process instance token to after the Review tweet activity task. The process should then move along to the Notify user of rejection task and finish. But before running the test, complete the updates to your code as shown next.

  3. Be sure to set an approved boolean variable to false in the map that you pass in along with the content of the tweet as you did earlier in testing. You’ll notice you won’t need to query and complete the Review tweet task since you’ll be starting after it.

  4. Add assertions about the expected state of the process. You can use .hasPassed("taskId") when the process instance should have passed a certain task.

        assertThat(processInstance).isEnded().hasPassed(findId("Tweet rejected"));
    

    Have a look at the camunda-bpm-assert user guide for further details. The new test method may look like this.

  5. Run your JUnit test. It should run both testHappyPath and testTweetRejected and generate coverage maps for each test as well as a summary coverage map for all tests. Open the generated TwitterQA.html coverage map and you should get either a 100% test coverage or close to it depending on how you modeled your process. All or most of your process artifacts should be green:

  6. Congrats, you’ve completed test coverage using process instance modification.

Optional

Extract the business logic from the java delegate into a separate bean, inject this to the service delegate and call the service method.

Get the tweet id from tweet.updateStatus() and save it in the process instance.

Change the JUnit test to mock this business service.

On this Page: