简体   繁体   English

Mockito:arg表示采用多个参数的方法

[英]Mockito: argThat for methods taking multiple arguments

I am trying to use Mockito's argThat API: 我正在尝试使用Mockito的argThat API:

verify(mockService).methodA(argThat((List ids, int b) -> ids.get(0).equals("123")));

mockService has methodA which takes two parameters: a List and a primitive integer. mockService具有methodA这两个参数:List和原始整数。

But this gives me an error: 但这给我一个错误:

"Imcompatible parameter types in lambda expression". “ lambda表达式中的参数类型不兼容”。

The reason is that ArgumentMatcher's matches method takes only one argument. 原因是ArgumentMatcher的matchs方法仅接受一个参数。

So how can I do verification for such scenarios? 那么,我该如何验证这种情况?

You should use an argThat wildcard for each of the inputs: 您应该为每个输入使用argThat通配符:

verify(mockService).methodA(argThat((List ids) -> ids.get(0).equals("123"))
          , argThat((int b) -> b < 1);

I would also suggest you use @ArgumentCaptor which is an alternative to argThat and makes that custom matching a bit more clear: javadoc . 我还建议您使用@ArgumentCaptor ,它是@ArgumentCaptor的替代argThat ,并使该自定义匹配更加清晰: javadoc Especially if you have to use both of the params at the same conditional statement. 特别是如果您必须在同一条件语句中同时使用这两个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM