I have a particularly nasty JMock checking() block for a JPA query that I want to migrate to Mockito:
Mockery jMock = new Mockery();
final EntityManager fakeEntityManager = jMock.mock(EntityManager.class);
final CriteriaBuilder fakeCriteriaBuilder = jMock.mock(CriteriaBuilder.class);
final CriteriaQuery<String> fakeCriteriaQuery = jMock.mock(CriteriaQuery.class);
jMock.checking(new Expectations() {{
oneOf(fakeEntityManager).getCriteriaBuilder(); will(returnValue(fakeCriteriaBuilder));
oneOf(fakeCriteriaBuilder).createQuery(String.class); will(returnValue(fakeCriteriaQuery));
oneOf(fakeCriteriaQuery).from(Archiveusergrouplicences.class);
oneOf(fakeCriteriaQuery).select(with(any(Selection.class)));
oneOf(fakeCriteriaBuilder).isNotNull(with(any(Expression.class)));
oneOf(fakeCriteriaQuery).where(with(any(Expression.class)));
oneOf(fakeEntityManager).createQuery(fakeCriteriaQuery);
// Return an empty resultset
}});
The code being tested looks like this:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> criteria = builder.createQuery(String.class);
Root<Archiveusergrouplicences> institution = criteria.from(Archiveusergrouplicences.class);
criteria.select(institution.get(Archiveusergrouplicences_.usergroupid));
criteria.where(builder.isNotNull(institution.get(Archiveusergrouplicences_.usergroupid)));
List<String> result = entityManager.createQuery(criteria).getResultList();
I've found this question on mocking builders, which goes some way to solving the CriteriaBuilder part of the mock; but my main problem is with using mocked objects as the .thenReturn() value of another mock - Mockito does not seem to allow that. For example, for the line:
CriteriaQuery<String> criteria = builder.createQuery(String.class);
I want to return the mock CriteriaQuery object, like this:
CriteriaQuery<String> fakeCriteriaQuery = mock(CriteriaQuery.class, RETURNS_DEEP_STUBS);
when(entityManager.createQuery(anyString())).thenReturn(fakeCriteriaQuery);
This throws a syntax error:
The method thenReturn(Query) in the type OngoingStubbing is not applicable for the arguments (CriteriaQuery)
How might I go about testing this code, or improving it to make it more testable?