I have some Roo-generated entities, with dynamic finders.
I'm trying to unit test a class that, basically, loads some data from different tables on the DB, runs some calculations and then outputs a structured object made of the results of these calculations, and i'd like to mock the persistence layer in order to be able to run the test without the DB (e.g. on Jenkins).
However, i can't find a simple solution to do this: i tried adding @MockStaticEntityMethods
, but the static methods for finders created by Roo return TypedQuery
instead of the actual entities, so i'm unable to pass the mocked objects to AnnotationDrivenStaticEntityMockingControl.expectReturn
.
I also tried using Mockito and Powermock, but i still can't seem to get over the problem that static finder methods return TypedQuery
and not the real entities.
What is the standard way of mocking Roo-generated finders, then?
In the end i solved this by creating a subclass of TypedQuery like this:
public class DummyTypedQuery implements TypedQuery
private T singleResult;
private List resultList;
public DummyTypedQuery(T singleResult) {
this.singleResult = singleResult;
}
public DummyTypedQuery(List resultList) {
this.resultList = resultList;
}
public DummyTypedQuery() {
}
@Override
public List getResultList() {
return resultList;
}
@Override
public Object getSingleResult() {
return singleResult;
}
...the rest of implemented methods, left blank
And setting it up to be returned by Roo finders with the AnnotationDrivenStaticEntityMockingControl; the DummyTypedQuery returns mock objects created programmatically, so i don't need to have test data in the DB (nor to actually have to connect to the DB) to run tests.
I was hoping to find some more elegant way to do this, but this looks like the only way.