Unit Test the Java Delegates

Hello,

I’m new to Camunda, and I need to test the Java Delegates in the solution. All that I’ve found out there are ways to test the process mocking the delegates. But what I want to create are test cases for the Delegate itself.

Is this something that can be done? If so, could you provide an example or point to a resource I can look at? Am I missing the point of unit testing processes in Camunda?

Thanks

1 Like

Hi @parroyo,

please check one of the following

does that help you?
Askar

Maybe, this code can help too,

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.test.ProcessEngineTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
public class GatherInformationTest extends ProcessEngineTestCase {

	@Autowired
	private GatherInformation gatherInformation;

	@Test
	public void testExecute() throws Exception {
		// preconditions
		DelegateExecution execution = mock(DelegateExecution.class);
		when(execution.getVariable("myVariable")).thenReturn("myValue");

		// execute unit under test
		gatherInformation.execute(execution);

		// postconditions
		assertThat(execution.getVariable("myVariable")).isEqualTo("myValue");
	}
}

Hi Osmar,

This one looks more simple. I’ll look into this. Thank you!

Hi Osmar / parroyo,

Can you please post sample code for junit testing.
I am facing some issue while calling delegateExecution.getVariableTypes , getting null pointer execption.

Hi, Were you able to get it work? I see here that “myVariable” is mocked to return myValue and that is asserted to true, which would be always true regardless of calling gatherInformation.execute(execution) .

I see here that “myVariable” is mocked to return myValue and that is asserted to true, which would be always true regardless of calling gatherInformation.execute(execution).

How can we make sure that gatherInformation.execute(execution) works as expected?

Assuming gatherInformation.execute(execution) performs some logic and uses the execution parameter, use verifications to check it actually invoked the correct methods on the execution.

For me most of the time the use case is, pass some execution variables and do some business log, get the JSON / XML kind of result then parse it, and set on the execution variables.

Since we mock execution and its variables, there is no way that the test would fail. any pointers on my case?

It would fail if you verify that some value is set on the execution. If the implementation is wrong, that would not be done and the verification would fail. It’s basic, but still.

but we mock complete execution, then it is tests responsibility to set those variables with expected values.

ex.
PowerMockito.when(taskExecution.getVariableLocal(“Status”)).thenReturn(“Success”); //mocking the variable to return success for status

Assert.assertEquals(“Success”,taskExecution.getVariableLocal(“Status”)); // verifying

–This is the reason I’m saying that the tests never fail.

I understand, but that’s also not what I mean. I mean to say that if you know your implementation should do something with the execution it is passed, it’s something you can verify against the mock. So, if your implementation should call e.g. execution.setVariable(“abc”, someObject), you can verify that it was actually done.

Not getting this, how can i verify that it is called?

With mocking frameworks, you can verify that certain interactions were executed with the mock itself. With Mockito for instance, assuming “execution” is a mock object you created, you can do:

verify(execution, times(1)).setVariable(“xyz”, expectedOrSomeValue);

That clears the doubt… Thank you.

You’re welcome :+1:

Can you please explain how to mock and verify the

ObjectValue preProcessedRecordsValue = Variables.objectValue(sitesAndServicesUploadVariable)
.serializationDataFormat(Variables.SerializationDataFormats.JAVA)
.create();

1 Like

Sorry no, I’m not even sure that part of the engine is pluggable so mocking it would be complicated. Why would you want to do that, if you can just put the actual value in the process?

You can add following dependency in your Source.

org.camunda.bpm.extension.mockito camunda-bpm-mockito test 4.12.0 it will provide you MockObjects like DelegateExecutionFake to be used in tests.

Ref.

have you found a solution to do this?