Mockito – 为 mock() 和 verify() 指定更详细的方法参数

huangapple go评论69阅读模式
英文:

Mockito - Specify more detailed method parameters for mock() and verify()

问题

我有以下的方法:

myMethod(QName param1, String[] param2, String... param3)

在我的测试中,我这样做:

Mockito.when(this.myMock.myMethod(Mockito.any(QName.class), Mockito.any(), Mockito.any()))
    .thenReturn(this.someOtherMock);

还有我这样进行验证:

Mockito.verify(this.myMock).myMethod(Mockito.any(QName.class), Mockito.any(), Mockito.any());

但是Mockito.any()太宽泛了,如何更详细地指定它必须是一个String数组,或者与String数组相同的省略号(...)?

在我的代码中,我调用myMethod如下:

myMethod(<..一些QName值..>, new String[]{"someValue"},  <String... 传递到外部方法的值所以它是一个数组>)
英文:

I have the following method

myMethod(QName param1, String[] param2, String... param3)

In my test I do

Mockito.when(this.myMock.myMethod(Mockito.any(QName.class), Mockito.any(), Mockito.any()))
.thenReturn(this.someOtherMock);

Also I do verify like this

Mockito.verify(this.myMock).myMethod(Mockito.any(QName.class), Mockito.any(), Mockito.any());

But Mockito.any() is to broad, how to specify more detailed that is must be an array of String, or elipsis of String (same as array)?

In my code I call the myMethod like

myMethod(&lt;..some QName value..&gt;, new String[]{&quot;someValue&quot;},  &lt;String... values passed into the outer method, so it&#39;s an array&gt;)

答案1

得分: 1

只返回翻译好的部分,不包括此注释部分
当你使用 Mockito 时,可以这样做:

Mockito.when(this.myMock.myMethod(Mockito.any(QName.class), ArgumentMatchers.<String[]>any(), ArgumentMatchers.<String[]>any())).thenReturn(this.someOtherMock);

英文:

You could do

Mockito.when(this.myMock.myMethod(Mockito.any(QName.class), ArgumentMatchers.&lt;String[]&gt;any(), ArgumentMatchers.&lt;String[]&gt;any())).thenReturn(this.someOtherMock);

huangapple
  • 本文由 发表于 2023年2月8日 21:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定