I am trying to add test coverage for following method which query for latest log date. I am not able to mock Query and assert the result.
public MyLogData findLastLogDate() {
Query query = new Query().with(Sort.by(Sort.Direction.DESC, "logDate"));
return mongoTemplate.find(query, MyLogDate.class).get(0); //Aautowired MongoTemplate
}
For test I am trying something like this
@Mock
private MongoTempalte mogoTemplate;
@InjectMocks
private MyClassToTest testMe;
@Test
void testQuery() {
List<MyLogData> logDataList = new ArrayList<>();
MyLogDate myLogDate = MyLogDate.builder()....build();
logDataList.add(myLogData);
when(mongoTemplate.find(mockQuery, mockMyLogData)).thenReturn(logDataList); //how to mock Query and MyLogData here?
MyLogData result = testMe.findLastLogDate();
assertEquals(myLogDate, result);
}
Any suggestion?