### Question:
AbstractCrudService extends AbstractDaoService<E, D> { @PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName())" + " or (#e.id == null and hasPermission(#e, 'CREATE'))" + " or (#e.id != null and hasPermission(#e, 'UPDATE'))") public void saveOrUpdate(E e) { dao.saveOrUpdate(e); } protected  AbstractCrudService(Class<E> entityClass); @PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName())" + " or (#e.id == null and hasPermission(#e, 'CREATE'))" + " or (#e.id != null and hasPermission(#e, 'UPDATE'))") void saveOrUpdate(E e); @PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#entity, 'UPDATE')") E updatePartialWithJsonNode(E entity, JsonNode jsonObject, ObjectMapper objectMapper); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(returnObject, 'READ')") @Transactional(readOnly = true) E findById(Integer id); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(returnObject, 'READ')") @Transactional(readOnly = true) E loadById(int id); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) List<E> findAll(); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") List<E> findAllRestricted(MultiValueMap<String, String> restrictToRequest); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) List<E> findBySimpleFilter(MultiValueMap<String, String> requestedFilter); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) List<E> findAllWhereFieldEquals(String fieldName, Object fieldValue); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) List<E> findAllWithCollectionContaining(String fieldName, PersistentObject subElement); @PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#e, 'DELETE')") void delete(E e);  }### Answer:
@SuppressWarnings("unchecked") @Test public void saveOrUpdate_shouldSave() { doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { E po = (E) invocation.getArguments()[0]; IdHelper.setIdOnPersistentObject(po, 1); return null; } }).when(dao).saveOrUpdate(implToTest); assertNull(implToTest.getId()); crudService.saveOrUpdate(implToTest); assertNotNull(implToTest.getId()); assertTrue(implToTest.getId() > 0); verify(dao, times(1)).saveOrUpdate(implToTest); }
@SuppressWarnings("unchecked") @Test public void saveOrUpdate_shouldUpdate() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InterruptedException { Integer id = 42; ReadableDateTime created = implToTest.getCreated(); ReadableDateTime modified = implToTest.getModified(); IdHelper.setIdOnPersistentObject(implToTest, id); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InterruptedException { E po = (E) invocation.getArguments()[0]; Thread.sleep(1); po.setModified(DateTime.now()); return null; } }).when(dao).saveOrUpdate(implToTest); crudService.saveOrUpdate(implToTest); assertEquals(id, implToTest.getId()); assertEquals(created, implToTest.getCreated()); assertTrue(implToTest.getModified().isAfter(modified)); verify(dao, times(1)).saveOrUpdate(implToTest); }