How to test delegateExecution variables in integration test

Hi, I can’t find an answer neither in camunda docs, nor on github.

I have integration test with autowired services, repositories, embedded db. Tasks are “mock registered”.
Somehow when I try to ger vars from processInstance in certain moments of workflow, e.g. it is waiting in one of the task, I don’t get this way the values of delegate execution variables:

VariableMap variableMap = ((ProcessInstanceWithVariablesImpl) processInstance).getVariables();

It contains everytime same two veriables like variableMap is always in one state in any moment of workflow, but I am 100% sure there should be more cause they were set.

So what is correct way to get actual variables from delegate execution in test? I think I do something wrong

@RunWith(JUnitParamsRunner.class)
@DataJpaTest
@Import(CommonTestConfig.class)
@TestPropertySource("classpath:application-test.properties")
public class CategorizedForNameMismatchTest extends BaseIntegrationTest {

    @Autowired
    private ReverifyDataService reverifyDataService;

    //other autowired components

    @Test
    @Parameters({
        TEMPLATE_CHARLIE_CONTACT_DATA1,
        TEMPLATE_CHARLIE_CONTACT_DATA2,
    })
    public void testExecute(String templatePath) throws Exception {
        PreLaunchCheckTask preLaunchCheckTask = new PreLaunchCheckTask();
        Mocks.register("preLaunchCheckTask", preLaunchCheckTask);

        ReflectionTestUtils.setField(preLaunchCheckTask, "processRepositoryService", processRepositoryService);
        ReflectionTestUtils.setField(preLaunchCheckTask, "reverifyDataService", reverifyDataService);
        ReflectionTestUtils.setField(preLaunchCheckTask, "preLaunchCheckService", preLaunchCheckService);

//other tasks reigstering with same approach

        ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("ReverifyContact", "mainProcessUUID",
                Map.of(BPMProcessConfigNames.assigneeSubJobUUID.configName(), "subProcessUUID"));

        assertThat(processInstance)
                .isStarted()
                .hasPassed("RV_STR")
                .hasPassed("PROCESS_INITIATED")
                .hasPassed("RV_CHK_GW")
                .hasPassed("RV_CHA_1")
                .isWaitingAt("RV_CHA_2");

        VariableMap variableMap = ((ProcessInstanceWithVariablesImpl) processInstance).getVariables();

        assertNull(variableMap.get(ProcessVariables.CharlieResponse.CHARLIE_DATA));
        assertEquals(false, variableMap.get(ProcessVariables.REQUIRES_TITLE_REVIEW));
        assertEquals("subProcessUUID", variableMap.get(BPMProcessConfigNames.assigneeSubJobUUID.configName()));

Hey @Ianis,

I would suggest cutting the tests differently, so you can keep them smaller and more isolated.

To test if your process works correctly (flow and the service task implementation) I suggest the following two kinds of tests:

  1. A process test to test the flow is correct and the correct JavaDelegate are called during a service task.
  2. Unit tests to check if the functionality of a JavaDelegate is correct (Calling the wright service, setting/reading the correct process variables).

The best practices for testing process definitions also describe different layers of testing. The documentation, with some helpful examples for Camunda Platform 7 could be you here.

I hope I got your question and could help :slight_smile:
Luc

Hi @lwluc Thanks for reply. No offense, but before posting this question I investigated all camunda docs and watch all videos related to tests. I can use any testing process definitions but my question is not about that.
Let me simplify it. How to check delegate execution variables value when workflow runs and stops in one of task to wait a message?
Can you share a knowledge how to get variables and their values in this certain moment?

runtimeService().getVariables / getVariablesLocal and other variations return empty lists

Hey @Ianis,

sorry, I still don’t really understand your question.
I hope this followup questions help: You want to test if a JavaDelegate reads and writes the process variables correctly. And you want to verify it, while your process instance is still running, right?

If so, one possibility your process variables are empty, would be a missing transaction boundaries. Querying the process variables using the RuntimeService they need to be persisted. Right now, I do not have any solution how you could query variables which are not persisted yet using the RuntimeService.

If I do not get your question right, a minimal example project might be helpful.

I hope this time I could provide a more helpful answer help :slight_smile:
Luc

@lwluc oh this is something. Yes, you understood correctly.
How I can turn on transactions boundaries for test? Is it possible for java delegate that is Mock.register()?

Hey @Ianis,

you can’t just turn the transaction boundaries on and off for a task during runtime. These must be explicitly modeled in the Camunda Modeler. Even if you could turn them on or off at runtime you would result in differences between your process model for testing and production.

Which brings me back to slicing your tests maybe a bit different. Have you read this blog post “Testing Process Dependencies”? I think the paragraph “Dependencies on source code” might help.

I hope with this information you could achieve your goal :slight_smile:
Luc

1 Like