Hello All,
I have the below flow which has a service task " Populate list Items" . This has a java delegate which create a transient json object variable and populates the data. Below is the code for it .In the call activity i iterate over the “items” collection to create new bpmn instance.
In the java delegate i have the below code
@Component("GetItemData")
public class GetItemData implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
String itemsList = "{\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\" : \"doe\",\n" +
" \"age\" : 26,\n" +
" \"items\": [\n" +
" {\n" +
" \"value\" : \"iPhone\",\n" +
" \"category\": \"mobile\"\n" +
" },\n" +
" {\n" +
" \"value\" : \"Pixel\",\n" +
" \"number\": \"mobile\"\n" +
" }\n" +
" ]\n" +
"}";
Spin itemDataJSON = JSON(itemsList);
TypedValue typedTransientObjectValue = Variables.objectValue(itemDataJSON, true).serializationDataFormat(Variables.SerializationDataFormats.JSON).create();
execution.setVariable("itemsData", typedTransientObjectValue);
}
}
Unit Test : - Ideally i would want to have to have a separate test case for the Java delegate and the test case for the “Process” . I have mocked the java delegate but the test case is failing at the “call activity” with message “Unknown property used in expression: ${itemsData.prop(“items”).elements()}. Cause: Cannot resolve identifier ‘itemsData’” since the Java delegate is mocked and its a void method for which i cannot put a Mockito.when().thenReturn()
@RunWith(MockitoJUnitRunner.class)
public class TestSampleFlow {
@Rule
public final ProcessEngineRule engine = new StandaloneInMemoryTestConfiguration().rule();
@Mock
private GetItemData getItemData;
@Before
public void setup() {
// register a bean name with mock expression manager
MockitoAnnotations.initMocks(this);
Mocks.register("GetItemData", getItemData);
}
@Test
@Deployment(resources = {"SampleFlow.bpmn", "process1"})
public void createSampleProcess() throws Exception {
RuntimeService runtimeService = engine.getRuntimeService();
Mockito.doNothing().when(getItemData).execute(any());
HistoryService historyService = engine.getHistoryService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("SampleFlow", "ORDER-01");
List<HistoricProcessInstance> processInstancesList = historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey("ORDER-01").list();
assertEquals(processInstancesList.size(), 3);
}
}
Please let me know if i am in the right path for unti Testing and if there are any work around to get this working .
Thanks
Dinesh