Here is a typical (Hibernate) JPA repository for saving an entity, a Cookie in this case:
@Entity
@Table(name = "cookie")
public class Cookie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_modified")
private Date lastModified;
}
@Repository
public class CookieRepository {
@PersistenceContext
EntityManager em;
@Autowired
TimeService timeService;
@Transactional
public Cookie createEntity(Cookie cookie) {
cookie.setLastModified(timeService.getTime().toDate());
em.persist(cookie);
return cookie;
}
}
Writing a unit test for this is not as straightforward as you might think. When a new cookie is created the id is null and only after it gets persisted in the database it gets assigned a value generated by the database.
The EntityManager persist method does not return a value, so how can we mock the entity manager _and_ set the id of the cookie?
Here is the answer:
@RunWith(MockitoJUnitRunner.class)
public class CookieRepositoryTest {
@Mock
EntityManager em;
@Mock
TimeService timeService;
@InjectMocks
CookieRepository underTest = new CookieRepository();
@Test
public void testCreateEntity() throws Exception {
Cookie newCookie = new Cookie();
when(timeService.getTime()).thenReturn(new DateTime(DateTimeZone.UTC));
doAnswer(new Answer<Cookie>() {
@Override
public Cookie answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
Cookie cookie = (Cookie) args[0];
cookie.setId(1);
return null;
}
}).when(em).persist(any(Cookie.class));
Cookie persistedCookie = underTest.createEntity(newCookie);
assertNotNull(persistedCookie.getId());
}
}
Mockito comes with the doAnswer method, specifically designed for dealing with void methods. With this method you can catch the input of the void method, persist() in this case, manipulate the object which than can be asserted for correctness later in the test.