I have an repository method that returns List, which under the hood uses CriteriaQueryTupleTransformer.TupleImpl. In tests I want to mock Repository and on Repository.method return predefined mocked data.
Something like this:
MyRepository myRepository = mock(MyRepository.class);
List<Tuple> = new ArrayList<>();
Tuple tuple = TupleImpl.Builder() //TupleImpl is private class and has no Factory or Builders
//.addMockedData()
//.addMockedData()
.build();
tuples.add(tuple);
//add more mocked data
when(myRepository.findByIds(any())).thenReturn(tuples);
//Assert business logic that everything
//went as expected when a specific Tuple structure was returned by repo
My main problem here is that I need to instantiate CriteriaQueryTupleTransformer.TupleImpl which is any private class, and I couldn't find any Builders or Factory methods for easy creation.
I did what you said in a comment. Answering here so people see what the solution looks like. Using Mockito.
private Tuple mockedTuple;
private List<Tuple> tupleList;
@Before
public void setUp() {
mockedTuple = mock(Tuple.class);
tupleList = new ArrayList<>();
tupleList.add(mockedTuple);
}
@Test
public void testTuple() {
when(mockedTuple.get(anyInt())).thenReturn(1);
when(repository.something(any())).thenReturn(tupleList);
// assertions
}