英文:
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(<..some QName value..>, new String[]{"someValue"}, <String... values passed into the outer method, so it's an array>)
答案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.<String[]>any(), ArgumentMatchers.<String[]>any())).thenReturn(this.someOtherMock);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论