CrmWebserviceEndpoint { @WebMethod public ContactDto updateContact(@WebParam(name = "contactDto") ContactDto contactDto) { Contact contact = contactDao.find(contactDto.getId(), new HashMap<String, Object>()); checkRevision(contactDto, contact); obtainContactMapper().mapContactWithoutHistoryInbound(contactDto, contact); incrementRevision(contact); contact = contactDao.merge(contact, new HashMap<String, Object>()); contactDto = new ContactDto(); obtainContactMapper().mapContactWithoutHistory(contact, contactDto); return contactDto; } CrmWebserviceEndpoint();  CrmWebserviceEndpoint(ContactDao contactDao); @Inject @WebMethod(exclude = true) void setContactDao(ContactDaoEjb contactDao); @WebMethod List<ContactDto> getContracts(@WebParam(name = "from") int from, @WebParam(name = "limit") int limit); @WebMethod ContactDto getContactIncludingHistory(@WebParam(name = "id") long id); @WebMethod ContactDto addContact(@WebParam(name = "contactDto") ContactDto contactDto); @WebMethod ContactDto updateContact(@WebParam(name = "contactDto") ContactDto contactDto); @WebMethod ContactDto addHistoryToContact(@WebParam(name = "contactDto") ContactDto contactDto, @WebParam(name = "historyDto") HistoryDto historyDto,
			@WebParam(name = "returnHistory") boolean returnHistory);  }
@SuppressWarnings("unchecked") @Test public void testUpdateContact() { when(contactDao.find(any(), anyMap())).thenReturn(contactRef); when(contactDao.merge(any(Contact.class), anyMap())).thenAnswer(new Answer<Contact>() { @Override public Contact answer(InvocationOnMock invocation) throws Throwable { Contact contactRet = (Contact) invocation.getArguments()[0]; Method idSetter = contactRet.getClass().getDeclaredMethod("setId", Long.TYPE); idSetter.setAccessible(true); idSetter.invoke(contactRet, 11); return contactRet; } }); contactDtoRef.setRevision(contactRef.getRev()); ContactDto contactDtoNew = webservice.updateContact(contactDtoRef); verify(contactDao).find(eq(new Long(11)), anyMap()); verify(contactDao).merge(argThat(new BaseMatcher<Contact>() { @Override public boolean matches(Object arg0) { return arg0 != null && (arg0 instanceof Contact) && ((Contact) arg0).getId() == 11 && ((Contact) arg0).getRev() == 3 && "Marge Simpson".equals(((Contact) arg0).getName()) && ((Contact) arg0).getHistory().size() == 1 && "mors, mors".equals(((Contact) arg0).getHistory().get(0).getText()); } @Override public void describeTo(Description arg0) { arg0.appendText("ID=11, Rev=3, Name=Marge Simpson, old hist"); } }), anyMap()); assertEquals(11, contactDtoNew.getId()); assertEquals(3, contactDtoNew.getRevision()); assertEquals("Marge Simpson", contactDtoNew.getName()); assertEquals(0, contactDtoNew.getHistory().size()); }
@SuppressWarnings("unchecked") @Test public void testUpdateContactInvalidRev() { when(contactDao.find(any(), anyMap())).thenReturn(contactRef); contactDtoRef.setRevision(contactRef.getRev() + 1); Exception exc = null; try { webservice.updateContact(contactDtoRef); } catch (Exception e) { exc = e; } verify(contactDao).find(eq(new Long(11)), anyMap()); verify(contactDao, never()).merge(any(Contact.class), anyMap()); assertNotNull(exc); assertTrue(exc.toString().contains("Try to work with outdated revision")); }