【问题标题】:Mockito: How to get the arguments passed to a method when the return type of the method is voidMockito:当方法的返回类型为void时如何获取传递给方法的参数
【发布时间】:2014-07-13 07:58:45
【问题描述】:

我读过this post,但我的问题是我的myFunction 返回void 而不是一个对象。所以我在

处遇到错误

when(mock.myFunction(anyString()))

when (java.lang.Void) in Mockito cannot be applied 
to   (void)

我该如何处理这个问题?

【问题讨论】:

  • 这个site 可以帮助你。
  • doNothing().when(mock).myFunction(anyString()) 适合你吗?

标签: java mockito


【解决方案1】:

我在对该问题的评论中有相同的答案,但只是为了让未来的读者更容易看到,在这里。

doNothing().when(mock).myFunction(anyString());

为了能够处理void返回类型。

【讨论】:

    【解决方案2】:

    这个问题的答案可以在我对您链接到的其他帖子的回答中找到。

    doAnswer(returnsFirstArg()).when(mock).myFunction(anyString());
    

    returnsFirstArg() 方法在 AdditionalAnswers 类中是静态的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2014-11-27
      • 2015-01-13
      • 1970-01-01
      • 2012-12-12
      相关资源
      最近更新 更多