簡體   English   中英

Mockito doReturn引發nullPointerException

[英]Mockito doReturn throws a nullPointerException

我正在嘗試模擬一個類的非靜態方法。 我正在測試同一個類的方法。 但是它返回一個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();

}}

我是否以錯誤的方式嘲笑了此方法?

b2p.evaluate(); 是在您的普通實例而不是模擬實例上調用的。

要調用您的mockSpy.evaluate();實例,您需要mockSpy.evaluate();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM