Mockito 的 argumentCaptor 示例 [英] Example of Mockito's argumentCaptor

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

问题描述

谁能为我提供一个示例,说明如何使用 org.mockito.ArgumentCaptor 类以及它与 mockito 提供的简单匹配器有何不同.

Can anyone please provide me an example showing how to use the 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, none of them 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 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆