mockito
Mockito ArgumentCaptor is used to capture arguments for mocked methods. ArgumentCaptor is used with Mockito verify() methods to get the arguments passed when any method is called. This way, we can provide additional JUnit assertions for our tests.
Mockito ArgumentCaptor用于捕获模拟方法的参数。 ArgumentCaptor与Mockito verify()方法一起使用,以获取调用任何方法时传递的参数。 这样,我们可以为测试提供其他JUnit断言 。
Mockito ArgumentCaptor (Mockito ArgumentCaptor)
We can create ArgumentCaptor instance for any class, then its capture() method is used with verify() methods.
我们可以为任何类创建ArgumentCaptor实例,然后将其capture()方法与verify()方法一起使用。
Finally, we can get the captured arguments from getValue() and getAllValues() methods.
最后,我们可以从getValue()和getAllValues()方法获取捕获的参数。
getValue() method can be used when we have captured a single argument. If the verified method was called multiple times then getValue() method will return the latest captured value.
当我们捕获单个参数时,可以使用getValue()方法。 如果多次调用经过验证的方法,则getValue()方法将返回最新捕获的值。
If multiple arguments are captured, call getAllValues() to get the list of arguments.
如果捕获了多个参数,请调用getAllValues()以获取参数列表。
Mockito ArgumentCaptor示例 (Mockito ArgumentCaptor Example)
Let’s say we have a class defined as:
假设我们有一个定义为的类:
class MathUtils {
public int add(int x, int y) {
return x + y;
}
public boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
}
return true;
}
public long squareLong(long l) {
return l*l;
}
}
We can write our test case and use ArgumentCaptor as shown below.
我们可以编写测试用例并使用ArgumentCaptor,如下所示。
@Test
void test() {
MathUtils mockMathUtils = mock(MathUtils.class);
when(mockMathUtils.add(1, 1)).thenReturn(2);
when(mockMathUtils.isInteger(anyString())).thenReturn(true);
ArgumentCaptor
acInteger = ArgumentCaptor.forClass(Integer.class);
ArgumentCaptor
acString = ArgumentCaptor.forClass(String.class);
assertEquals(2, mockMathUtils.add(1, 1));
assertTrue(mockMathUtils.isInteger("1"));
assertTrue(mockMathUtils.isInteger("999"));
verify(mockMathUtils).add(acInteger.capture(), acInteger.capture());
List
allValues = acInteger.getAllValues();
assertEquals(List.of(1, 1), allValues);
verify(mockMathUtils, times(2)).isInteger(acString.capture());
List
allStringValues = acString.getAllValues();
assertEquals(List.of("1", "999"), allStringValues);
}
Mockito @Captor (Mockito @Captor)
We can use @Captor annotation to create argument captor at field level. So instead of initializing field level ArgumentCaptor as:
我们可以使用@Captor批注在字段级别创建参数捕获器。 因此,与其将字段级别的ArgumentCaptor初始化为:
ArgumentCaptor
acLong = ArgumentCaptor.forClass(Long.class);
We can use @Captor as:
我们可以将@Captor用作:
@Captor ArgumentCaptor
acLong;
Note that we have to call MockitoAnnotations.initMocks(this); before test methods to get it initialized by Mockito framework.
注意,我们必须调用MockitoAnnotations.initMocks(this); 在通过Mockito框架对其进行初始化的测试方法之前。
Mockito @Captor示例 (Mockito @Captor Example)
Here is a simple example of @Captor annotation.
这是@Captor批注的简单示例。
class MockitoArgumentCaptorExamples {
@Captor ArgumentCaptor
acLong;
@Test
void test() {
MathUtils mockMathUtils = mock(MathUtils.class);
when(mockMathUtils.squareLong(2L)).thenReturn(4L);
assertEquals(4L, mockMathUtils.squareLong(2L));
verify(mockMathUtils).squareLong(acLong.capture());
assertTrue(2 == acLong.getValue());
}
}
翻译自: https://www.journaldev.com/21892/mockito-argumentcaptor-captor-annotation
mockito
Mockito ArgumentCaptor 是一种用于捕获模拟方法参数的工具,常与 verify 方法结合,便于测试中进行额外的断言。@Captor 注释则允许在字段级别创建参数捕获器,简化代码。通过 ArgumentCaptor,可以获取调用方法时传递的参数值,对于单个参数和多个参数的捕获提供了相应的方法。
8515

被折叠的 条评论
为什么被折叠?



