本文介绍了关于Mockito的argumentCaptor的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请我提供一个示例,说明 org.mockito.ArgumentCaptor 类的用法以及它与简单匹配器的不同之处这是与mockito一起提供的。

Can anyone please provide me an example showing what is the use of org.mockito.ArgumentCaptor class and how it is different from simple matchers that are provided with mockito.

我阅读了提供的mockito文件,但这些文件没有清楚地说明,
他们也无法清楚地解释它。

I read the provided mockito documents but those doesn't illustrate it clearly,neither they are able to explain it with clarity.

推荐答案

我同意@fge所说的更多内容。让我们看一下例子。
考虑你有一个方法:

I agree with what @fge said, more over. Lets look at example.Consider you have a method:

class A {
    public void foo(OtherClass other) {
        SomeData data = new SomeData("Some inner data");
        other.doSomething(data);
    }
}

现在,如果你想查看内部数据,你可以使用捕获器:

Now if you want to check the inner data you can use the captor:

// Create a mock of the OtherClass
OtherClass other = mock(OtherClass.class);

// Run the foo method with the mock
new A().foo(other);

// Capture the argument of the doSomething function
ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
verify(other, times(1)).doSomething(captor.capture());

// Assert the argument
SomeData actual = captor.getValue();
assertEquals("Some inner data", actual.innerData);

这篇关于关于Mockito的argumentCaptor的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:21