Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito doReturn抛出一个nullPointerException_Java_Mockito_Stubbing - Fatal编程技术网

Java Mockito doReturn抛出一个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<

我试图模拟一个类的非静态方法。我正在测试同一类的方法。但它返回一个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;
    }
}
} }

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

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();