Camunda BPM Assert - async java delegate - AbstractBpmnActivityBehavior - Unit test

Hi

I am looking for a way to test a process which contain a task with a java delegate which is extending AbstractBpmnActivityBehavior. This java delegate send a message on a Kafka queue and then waits to resume via the call to the signal method. This works fine. But, i am having a hard time to make a useful junit test for it.

i have read and looked into your Camunda BPM Assertj docs and code here. Have seen that you have made a lot of good work there the last weeks:

and here:
https://github.com/camunda/camunda-bpm-assert/blob/master/docs/User_Guide_BPMN.md

But, my task is in the middle of the BPMN process flow. Is it possible to test just that task? Without running the whole process?

Would be grateful for any hints or tips.

Br

Frank

Hi Frank,

maybe this might be of help: https://docs.camunda.org/manual/7.10/user-guide/process-engine/process-engine-concepts/#start-a-process-instance-at-any-set-of-activities

Best,
Tobias

Hi

Many thanks for your answer.

I have tested it now. There is one problem. When i start my task like this:

BlockquoteProcessInstance processInstance = runtimeService().createProcessInstanceByKey(“Casework”)
.startBeforeActivity(“Task_18gi722”)
.setVariable(Consts.CALL_ID, “123”)
.setVariable(KafkaUUIDRequestInterceptor.NCMS_CORRELATION_ID, “999”)
.businessKey(businessKey)
.execute();

I get this error when running junit test:

Blockquote
ENGINE-16004 Exception while closing command context: Unknown property used in expression: ${createJavaDelegate}. Cause: Cannot resolve identifier ‘createJavaDelegate’

I believe this is because I have wired the java delegate with an expression and not with the java class path. I have done like this in camunda modeller: ${createJavaDelegate}

It works fine when I run the code. But not when I am running junit tests.

@jangalinski

Hi. Maybe you have some good hints on how to junit test java delegates that are wired by expression? See that you have made changes to camunda-bpm-mockito lately.

Br

Frank

Hi Frank,

what is ${createJavaDelegate} supposed to be resolved to?

Without knowing more about your process definition this rather looks like a variable that has not been set yet because the activities before the task are not executed now. If that is the case you could manually provide the value for this test case as you already did with .setVariable("createJavaDelegate", whatEverNeedsToGoHere).

Is that of any help?

Best,
Tobias

Hi

It resolves to a class that extends AbstractBpmnActivityBehavior like this:

Blockquote @Component(“createJavaDelegate”)
public class CreateJavaDelegate extends AbstractBpmnActivityBehavior {

Now I am trying to test the CreateJavaDelegate via junit. Like this:

Blockquote @RunWith(SpringJUnit4ClassRunner.class)
public class CreateJavaDelegateTest {

private final DelegateExecutionFake delegate = CamundaMockito.delegateExecutionFake();

@Autowired
CreateJavaDelegate createJavaDelegate;



@Test
public void testGetKafkaDTO() throws Exception {
    DelegateExecution execution = new DelegateExecutionFake();

    delegate.setVariable(Consts.ID,"123");
    delegate.setVariable(Consts.RESULT,"false");

    createJavaDelegate.getKafkaDTO(delegate, "ehValidateValue");
}

I try to test the getKafkaDTO method in the CreateJavaDelegate class.

Is this the correct way to do it?

If you want to just test the delegate, the way you are working with the DelegateExecutionFake is the right way to go. Do you have problems with that unit test?

For the “complete process” test, it dependes if you use the plain in-memory engine or Spring. In the first case, you are using a MockExpressionManager, meaning that you have to manually register every expression (Mocks.register(“myDelegate”, mock(MyDelegate.class))) so camunda can resolve it. CamundaMockito gives some nice helpers for this, but basically its always registering a String/Instance Pair on the MockExpressionManager.

When doing a spring based process test, you do use the SpringExpressionManager, so every bean available in the spring context could be a candidate for bpmn-expressions. If this fails, check if you configured the context for your test run correctly.