@Test public void shouldSaveOrdersToEncounterWithExistingOrders() throws ParseException { EmrOrderServiceImpl_1_10 emrOrderService = new EmrOrderServiceImpl_1_10(openMRSDrugOrderMapper, encounterService, openMRSOrderMapper); EncounterTransaction.Order order1 = new OrderBuilder().withConceptUuid("concept-uuid1").withComment("Comment").build(); EncounterTransaction.Order order2 = new OrderBuilder().withConceptUuid("concept-uuid2").withComment("Comment").build(); Order mappedOrder1 = new Order(); Concept concept = new Concept(); concept.setUuid("concept-uuid1"); mappedOrder1.setConcept(concept); mappedOrder1.setCommentToFulfiller("Comment"); Order mappedOrder2 = new Order(); concept = new Concept(); concept.setUuid("concept-uuid2"); mappedOrder2.setConcept(concept); mappedOrder2.setCommentToFulfiller("Comment"); Order existingOrder1 = new Order(); Order existingOrder2 = new Order(); Encounter encounter = new Encounter(); encounter.addOrder(existingOrder1); encounter.addOrder(existingOrder2); when(openMRSOrderMapper.map(order1, encounter)).thenReturn(mappedOrder1); when(openMRSOrderMapper.map(order2, encounter)).thenReturn(mappedOrder2); emrOrderService.saveOrders(Arrays.asList(order1, order2), encounter); ArgumentCaptor<Encounter> encounterArgumentCaptor = ArgumentCaptor.forClass(Encounter.class); verify(encounterService).saveEncounter(encounterArgumentCaptor.capture()); Encounter savedEncounter = encounterArgumentCaptor.getValue(); ArrayList<org.openmrs.Order> savedOrders = new ArrayList<org.openmrs.Order>(savedEncounter.getOrders()); assertThat(savedOrders.size(), is(4)); assertTrue(existsInOrdersList(mappedOrder1, savedOrders)); assertTrue(existsInOrdersList(mappedOrder2, savedOrders)); }