Hi there, I have a model where a call-activity is invoked at a certain place, and I want to implement a unit test for it. Both the main and the sub process are deployed with the @Deployment annotation.
When the test is performing, it gets stuck at the call activity - I could prove because isWaitingAt(“subProcess”) returned true. So, I wanted to go on from there, and implemented execute(job()). This failed with a NullPointerException because job() returned null. What did I miss?
the job() method looks for the one job in the master process instance.
Please have a look at the other job(...) methods that BPM Assert provides. You can also pass on a processInstance for example or your own jobQuery.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestCamundaConfiguration.class})
public class MyTest {
private ProcessScenario processScenario = mock(ProcessScenario.class);
@Before
public void setup() {
when(processScenario.waitsAtMockedCallActivity("MyCallActivityIdTheOneWillCallASubProcess"))
.thenReturn(callActivity -> callActivity.complete(Collections.singletonMap(
"status", "1"))); // "status" is a sub-process-outgoing parameter required for the main process
}
@After
public void resetMocks() {
Mocks.reset();
}
private ProcessInstance startProcess() {
final VariableMap variables = Variables.createVariables()
.putValue("request_id", "1")
.putValue("request_colour", "blue");
final Scenario scenario = Scenario.run(this.processScenario)
.withMockedProcess("SubProcessNameId") // mocking sub process
.startByKey("MainProcessNameId", variables)
.execute();
return scenario.instance(this.processScenario);
}
@Test
public void happyFlow() {
final ProcessInstance processInstance = startProcess();
// task2 is part of the sub process that was mocked
assertThat(processInstance).hasPassedInOrder("start_event", "task1", "call_sub_process","task3");
}
In my case the process has multiple call activities. I mocked the first one by using @abelaneiros code and wanted to check, if the process waits at the second call activity afterwards (after passing some other user tasks in between).
However, I encountered the following exception:
java.lang.AssertionError: Process Instance {my-main-process:1:3, 11} waits at an unexpected CallActivity ‘my-second-call-activity’.
What’s even more confusing to me, is, that this activity is exactly where I expect the process to be:
I would say you are getting Process Instance {my-main-process:1:3, 11} waits at an unexpected CallActivity ‘my-second-call-activity’ error because “my-second-call-activity” is not defined (or deployed) in your test context. Could you check that?
Could you share the definition of my-second-call-activity? Could you check that the “Called element” has my-second-subprocess?
Thanks for the input. I already tried to deploy it, with the following outcome, so I removed it again:
java.lang.AssertionError: Process ‘my-second-subprocess’ declared to be mocked, but it is already deployed. Please remove from your list of explicit deployments.
Did you try deploying it and removing the mocking part? I think this is the fix. I mocked my process because I did want to skip it, like your first sub-process, but if what you need if to check your current process is waiting for the second one execution, I wouldn’t mock it.
Try removing this line and deploying your sub-process:
in your setup you have to configure the process engine for autodeployment of process models.
The @Deployment annotations requires a process engine managed by the JUnit test, as it is described for different JUnit versions here: Testing | docs.camunda.org
Hi @abelaneiros
thank you very much for your effort! I wasn’t able to solve the problem in my current environment (yet), however, I’m going to set up a new (smaller) test project based on your code to hopefully get this to work