gpt4 book ai didi

java - Mockito doReturn引发nullPointerException

转载 作者:行者123 更新时间:2023-12-03 10:06:59 25 4
gpt4 key购买 nike

我正在尝试模拟一个类的非静态方法。我正在测试同一个类的方法。但是它返回一个nullPointerException。想知道我在这里做错了什么。任何帮助将不胜感激。

我的代码:

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;
}
}

我的测试课:
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();

}
}

我是否以错误的方式 mock 了此方法?

最佳答案

b2p.evaluate();在您的普通实例上调用,而不是在模拟实例上调用。

要调用模拟实例,您需要mockSpy.evaluate();

关于java - Mockito doReturn引发nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com