Add Process Variables and Process Test Coverage

Goal

In this lab you will change your JavaDelegate to read the content from a process variable rather than a hard coded value. Then we’ll update the test to send in the content of a tweet. Then we’ll show you how to set up process test coverage to visualize your test cases in the context of a process diagram.

Short description

  • Add the content at the process instance start.
  • Use the content in the Java Delegate to send it to Twitter and test your changes.
  • Add the dependency to the process test coverage: org.camunda.bpm.extension:camunda-bpm-process-test-coverage-junit5:1.0.0
  • Change the extension to the ProcessEngineCoverageExtension
  • Change the process engine configuration in the engine configuration file to ProcessCoverageInMemProcessEngineConfiguration
  • Run the test and inspect the test coverage.
  • Think about strategies to test if you pass by the CreateTweetDelegate (and maybe skip the call to Twitter to make a real unit test).

Detailed steps

  1. In the previous lab, the code used a hardcoded value for the tweet in the java delegate. Now we’ll update the java delegate code to retrieve the tweet content from a variable and then tweet it out. Go back to the CreateTweetDelegate code and update the following code

    String content = "I did it! Cheers YOUR NAME HERE";

    If you look at the execute method where this line resides, you’ll notice that it passes in the DelegateExecution execution object. The execution object represents the context of the process instance and will allow you to access things such as variables in the process. Change the hardcoded value to:

    String content = (String) execution.getVariable("content");

    This will retrieve the value of “content” which should have been set earlier in the process. The getVariable() method returns an object hence you’ll need to cast it from and object to a string.

  2. Next, go back to the JUnit test case you created earlier and add the “content” variable to the HashMap in the testHappyPath method:

    variables.put("content", "Exercise 4 test - YOUR NAME HERE");

  3. Run the JUnit test case. Remember how to do it? You should see the content from the test case on Twitter. What happens if you run the test again without changing the tweet content?

Adding Process Engine Test Coverage

An extension that can help you visualize your test cases and what branches of the process have been covered by your test cases can be included in your project. Follow these directions to add process test coverage to your project.

  1. Add the dependency to the process engine test coverage in your pom.xml preferably past line 94 <!-- Add your own dependencies here, if in compile scope, they are added to the jar -->:

        <dependency>
          <groupId>org.camunda.bpm.extension</groupId>
          <artifactId>camunda-bpm-process-test-coverage-junit5</artifactId>
          <version>1.0.0</version>
          <scope>test</scope>
        </dependency>
    
  2. In your JUnit test replace your old ProcessEngineExtension with the ProcessEngineCoverageExtensionlike below:

    @ExtendWith(ProcessEngineCoverageExtension.class)
    

    Your IDE may prompt you to add the appropriate ProcessEngineCoverageExtension import.

  3. If you are using camunda.cfg.xml change the configuration class in the camunda.cfg.xml. The class for the processEngineConfiguration has to be org.camunda.bpm.extension.process_test_coverage.engine.ProcessCoverageInMemProcessEngineConfiguration from now on.

  4. Run the JUnit test. You may want to think about how to make the content of the tweet unique each time you run the test. Random numbers? System current time millis? Or even a more sophisticated approach? Up to you.

  5. Once the test is complete navigate to target/process-test-coverage/com.camunda.training.ProcessJUnitTest (or whatever you called your test case). You should find an html file called report.html. Right click on the file and select Open with > Web Browser. In the report you can click on your test class and click on the process model. You should see something like this detailing the happy path test case. As you create other test cases you’ll see other tasks in the process turn green.

Optional

Skip the call to Twitter to make a real unit test.

On this Page: