springspring-bootbpmncamunda

Spring boot camunda unit tests - async before/after


I have model have nested call activities. I created unit test using spring boot camunda starter. Test looks like

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DiagramTest {


    @Autowired
    private ProcessEngine processEngine;

    @Rule
    @ClassRule
    public static ProcessEngineRule rule;

    @PostConstruct
    void initRule() {
        rule = TestCoverageProcessEngineRuleBuilder.create(processEngine).build();
    }

    @Before
    public void setup() {
        ProcessEngineTests.init(processEngine);
    }

    @Test
    @Deployment(resources = {"process.bpmn", "subprocess.bpmn", "sub-subprocess.bpmn"})
    @RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_NONE)
    public void ruleUsageExample() {


        RuntimeService runtimeService = rule.getRuntimeService();
 runtimeService.startProcessInstanceByKey("process"); }

My problem is that on most of the components I use async before/after and because of that subprocesses do not start. When I remove async-s it works as expected. What should be the proper way of testing async behaviour in camunda bpmn?


Solution

  • I'm not exactly sure this will solve your problem, but it wouldn't fit in a comment.

    The problem here is that the process instance is persisted and then executed via a next Thread from the threadpool, so your thread which executes the unit tests ends. You need to test if the job is finished via query to the process engine and execute the job manually. I'm using this utility method:

    public boolean waitUntilNoActiveJobs(ProcessEngine processEngine, long wait) {
        long timeout = System.currentTimeMillis() + wait;
        while (System.currentTimeMillis() < timeout) {
            long jobCount = processEngine.getManagementService().createJobQuery().active().count();
            if (jobCount == 0) {
                return true;
            }
            final List<Job> jobs = processEngine.getManagementService().createJobQuery().list();
            jobs.forEach(job -> processEngine.getManagementService().executeJob(job.getId()));
            System.out.println("Waiting for " + jobs + " jobs");
        }
        return false;
    }
    

    You call it at the end of your test, the parameters seem to be obvious.