### Question:
JpaUtil { public static <T, I> T saveOrUpdate(I id, EntityManager entityManager, T entity) { if (id == null) { entityManager.persist(entity); return entity; } else { return entityManager.merge(entity); } } private  JpaUtil(); static T getSingleResult(List<T> list); static T saveOrUpdate(I id, EntityManager entityManager, T entity); static List<T> getPagedResultList(TypedQuery<T> query, int offset, int pageSize); static void clearAndAdd(List<T> target, List<E> newList, Class<T> clazz);  }### Answer:
@Test public void saveOrUpdate_save() { Object o = new Object(); mockManager.persist(o); expectLastCall(); replay(mockManager); Object r =JpaUtil.saveOrUpdate(null, mockManager, o); assertThat(r, is(sameInstance(o))); verify(mockManager); }
@Test public void saveOrUpdate_update() { Object o = new Object(); expect(mockManager.merge(o)).andReturn(new Object()); replay(mockManager); Object r = JpaUtil.saveOrUpdate(new Object(), mockManager, o); assertThat(r, is(not(sameInstance(o)))); verify(mockManager); }