java Mockito 间谍测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10287746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:23:58  来源:igfitidea点击:

Mockito Spy Test

javaspringmockito

提问by aces.

I am using Mockito to write tests for code. However I am stuck at following scenario - Class A has 2 methods, method1() and method2(). I tried using ArgumentCaptor to catch values sent to method2(). But, since I am using @Spy, I cannot use Matchers.

我正在使用 Mockito 编写代码测试。但是我被困在以下场景中 - A 类有 2 个方法,method1() 和 method2()。我尝试使用 ArgumentCaptor 来捕获发送到 method2() 的值。但是,因为我使用的是@Spy,所以我不能使用匹配器。

How do I test method1()?

我如何测试method1()?

class A{
    B b;
    method1(arg1, arg2){
       //some logic
       method2(arg1, arg2, ....argN);
    }

   method2(arg1, arg2,....argN){
       //some logic
       b.method3(arg1, arg2...);
   }
}

How to verify method2 receives same argument values? Following is the test class I wrote:

如何验证method2接收相同的参数值?以下是我写的测试类:

Class TestA{

@Mock
B b;

@Spy
@InjectMocks   //required else b is null
A a = new A();

@Test
public void testMethod1(){

 a.method1(arg1, arg2);

  //How to verify method2 receives same argument values (arg1, arg2)????
  //verify(a, times(1)).method2(.......);   
}

}

}

回答by Brad

I was intrigued by this post and the comments left by @David so I decided to code a working example for those who follow like me

我对这篇文章和@David 留下的评论很感兴趣,所以我决定为那些像我一样关注的人编写一个工作示例

/*
 * class to test
 */
public class A {

    public void methodA(String str) {
        this.methodB(str);
    }

    protected void methodB(String str) {
        // some implementation
    }
}

We want to assert that the value being passed into methodB() is what we expect. Reading up on the ArgumentCaptorled me to discover the equivalent Captor Annotation

我们想断言传递给 methodB() 的值是我们所期望的。阅读ArgumentCaptor让我发现了等效的Captor Annotation

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MultipleMethodCallTest {

    @Spy
    A a = new A();

    @Captor ArgumentCaptor<String> captor;

    @Test
    public void captureSecondMethodCallArgument() throws Exception {

        // EXPECTED

        String greeting = "hello world";

        // PERFORM TEST

        a.methodA(greeting);

        // ASSERT

        verify(a).methodB(captor.capture());

        assertEquals(greeting, captor.getValue());
    }
}

This example was tested with

这个例子是用

  • mockito-all-1.8.5.jar
  • junit-4.8.2.jar
  • mockito-all-1.8.5.jar
  • junit-4.8.2.jar

回答by Omnaest

You cant, you have to verify it by B's method3 call. If your args to method2 have no effect on method3, theses args could be useless at all?!

你不能,你必须通过 B 的 method3 调用来验证它。如果你对 method2 的 args 对 method3 没有影响,这些 args 可能根本没用?!

回答by Dawood ibn Kareem

You can use matchers with spies; this works just fine. I don't know why you thought that you couldn't.

您可以将匹配器与间谍一起使用;这工作得很好。我不知道你为什么认为你不能。

I took your source code and edited it to make it compile. I then added a call to MockitoAnnotations.initMocks- you need this to create the spy and the mock, and to inject the mock (unless you use the MockitoJUnitRunner, which does the initMocksfor you). I put the verifyof the call to method2back in. This worked fine.

我拿了你的源代码并编辑它以使其编译。然后我添加了一个调用MockitoAnnotations.initMocks- 你需要它来创建间谍和模拟,并注入模拟(除非你使用MockitoJUnitRunner,它initMocks为你做)。我把verify电话放method2回去了。这很好用。

So, contrary to Omnaest's answer, you don't need to use B's method3to verify this. I suspect that your only problem was that you had forgotten the initMocks.

因此,与 Omnaest 的回答相反,您不需要使用 Bmethod3来验证这一点。我怀疑你唯一的问题是你忘记了initMocks.

Good luck with this, and feel free to post again if you need any more help.

祝您好运,如果您需要更多帮助,请随时再次发布。