Java Mockito doReturn抛出一个nullPointerException
我试图模拟一个类的非静态方法。我正在测试同一类的方法。但它返回一个nullPointerException。不知道我做错了什么。任何帮助都将不胜感激 我的代码:Java Mockito doReturn抛出一个nullPointerException,java,mockito,stubbing,Java,Mockito,Stubbing,我试图模拟一个类的非静态方法。我正在测试同一类的方法。但它返回一个nullPointerException。不知道我做错了什么。任何帮助都将不胜感激 我的代码: public class B2P { protected Future<List<FeedObject>> evaluate (NodeContext nodeContext) throws Exception { Map<BoardQuery, Future<
public class B2P {
protected Future<List<FeedObject>> evaluate (NodeContext
nodeContext) throws Exception {
Map<BoardQuery, Future<List<FeedObject>>> resultsFuturesMap =
getResultsFutureMap(boardsQuery,
query, generatorParams, resourceContainer);
return resultsFuturesMap;
}
public Map<BoardQuery, Future<List<FeedObject>>>
getResultsFutureMap(){
DoSomething;
}
}
}
}
我是否以错误的方式模拟了此方法?b2p.evaluate()代码>是在普通实例上调用的,而不是在模拟实例上调用的
要调用模拟实例,您需要mockSpy.evaluate()代码>该方法实际上会被调用,而不会被模拟。
public class testClass {
public void test() {
B2P b2p = new B2P();
B2P mockSpy = Mockito.spy(b2p);
doReturn(ImmutableMap.of(
new BoardQuery().setBoardId(102L).setSignatureWeights(
ImmutableMap.of("s3", 1.0, "s4", 1.0, "s5", 1.0)),
Future.value(ImmutableList.of(
new FeedObject().setObjectId(904L).setObjectDetails(new
ObjectDetails()
.setCommonObjectDetails(new CommonObjectDetails())
.setPinDetails(new
PinDetails().setSource(FeedSourceType.FANTASIO))),
new FeedObject().setObjectId(903L).setObjectDetails(new
ObjectDetails()
.setCommonObjectDetails(new CommonObjectDetails())
.setPinDetails(new PinDetails().setSource(FeedSourceType.FANTASIO))),
new FeedObject().setObjectId(902L).setObjectDetails(new ObjectDetails()
.setCommonObjectDetails(new CommonObjectDetails())
.setPinDetails(new PinDetails().setSource(FeedSourceType.FANTASIO)))))))
.when(mockSpy).getResultsFutureMap(any(), any(), any(), any());
....
b2p.evaluate();