我正在寻找模拟 MongoTemplate,以便可以实际调用我的服务中的executeQuery 函数。
这就是我的服务代码块的样子:
this.mongoTemplate.executeQuery(query, collectionName, new DocumentCallbackHandler() {
@Override
public void processDocument(Document document) throws MongoException, DataAccessException {
try {
// convert to strongly typed object
System.out.println("Actual method called");
ProductDayData pdd = mongoTemplate.getConverter().read(ProductDayData.class, document);
// call copyover
backFillOneEvent(pdd, liveContractIds, pddType, toDate, now, timeAtStartOfDay);
} catch (RuntimeException e) {
LOGGER.error("FATAL: backfill - error processing document: {}", document, e);
} catch (Throwable t) {
LOGGER.error("FATAL: backfill - error processing document: {}", document, t);
throw t;
}
}
});
在我的单元测试中,这就是我所做的:
@Mock
private MongoTemplate mongoTemplate;
service = new MyService(mongoTemplate); //mock is set
Mockito.doCallRealMethod().when(mongoTemplate).executeQuery(Mockito.any(Query.class),Mockito.any(String.class),Mockito.any(DocumentCallbackHandler.class));
doCallRealMethod() 显然没有做任何事情,我期望它触发实际的方法。