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

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

问题描述

任何人都可以给我提供一个示例,演示如何使用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的argumentsCaptor示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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