How to create focused unit tests for complex models


#1

Is it possible to create unit tests for only a given section of a model by defining the beginning and the end of the test?

I found
runtimeService().createProcessInstanceByKey("...").startBeforeActivity("...activity id ...")
that allows starting the test at any given point of the model, which is great.

Is it possible to ignore the rest of the model after a given activity?
I also found
runtimeService().createProcessInstanceModification("...processInstanceId...").cancelAllForActivity(...)

This seems to be very handy too, but what can I do if my model does not have user tasks and all the rest of the model gets executed before I could get the execution back?

Thank you very much for your suggestions.
Gabor


#2

I’d recommend directly invoking your task implementations (e.g. delegate implementations).

This implies that you’re discretely testing various sub-sets of the model.


#3

Hi @janigabor,

You can start the test with the help of process instance modification api whereever you want: [1]

runtimeService.createProcessInstanceByKey("key").startAfterActivity("someTask").setVariables(myMap).execute();

The process always runs until the next wait state [2]. You can go further by completing the task or assert the desired result here.

1: https://docs.camunda.org/manual/7.7/user-guide/process-engine/process-instance-modification/#process-instance-modification-in-junit-tests
2: https://docs.camunda.org/manual/7.7/user-guide/process-engine/transactions-in-processes/#wait-states

Cheers, Ingo


#4

That’s exactly the point. I don’t want my process to go on with execution after a given point. But I don’t have user tasks so it does not go into wait state.

So is there a way to modify the process dynamically? Remove activities from it that I don’t want to test perhaps?

Or is there a better approach to create a focused unit test for a part of a process?

Thank you.
G


#5

Hello @janigabor,

may it help to mark the last activity or event of the test with ‘async after’?

If you want to do it for the test only, you can use the model-api to change the model before the deployment.

Does it help you?


#6

I had hoped for a better solution, but this works too.
Thanks