简体   繁体   English

Mockito 验证被监视对象方法的返回

[英]Mockito verify the return of a spied object method

I know you can verify the times a spied object's method was called.我知道你可以验证被监视对象的方法被调用的次数。 Can you verify the result of the method call?你能验证方法调用的结果吗?

Something like the following?像下面这样的?

verify(spiedObject, didReturn(true)).doSomething();

To verify the number of times it was invoked, use verify(spiedObject, times(x)).doSomething() . 要验证它被调用的次数,请使用verify(spiedObject, times(x)).doSomething()

You should NOT be verifying the value returned from the spied object. 您不应该验证从间谍对象返回的值。 It is not the object under test so why verify what it returns. 它不是被测试的对象,所以为什么要验证它返回的内容。 Instead verify the behavior of the object under test in response to the value returned from the spy. 而是验证被测对象的行为以响应从间谍返回的值。

Also, if you don't KNOW what value will be returned by the spied object it would be better to use a mock instead of a spy. 此外,如果您不知道间谍对象将返回什么值,那么最好使用模拟而不是间谍。

TL;DR TL; 博士
I am providing a template for tests where you want to verify what SpyBean method is returning.我提供了一个测试模板,您可以在其中验证SpyBean方法返回的内容。 The template is using Spring Boot.该模板使用 Spring Boot。

@SpringJUnitConfig(Application.class)
public class Test extends SpringBaseTest
{
    @SpyBean
    <replace_ClassToSpyOn> <replace_classToSpyOn>;

    @InjectMocks
    <replace_ClassUnderTest> <replace_classUnderTest>;

    // You might be explicit when instantiating your class under test.
    // @Before
    // public void setUp()
    // {
    //   <replace_classUnderTest> = new <replace_ClassUnderTest>(param_1, param_2, param_3);
    // }

    public static class ResultCaptor<T> implements Answer
    {
        private T result = null;
        public T getResult() {
            return result;
        }

        @Override
        public T answer(InvocationOnMock invocationOnMock) throws Throwable {
            result = (T) invocationOnMock.callRealMethod();
            return result;
        }
    }

    @org.junit.Test
    public void test_name()
    {
        // Given
        String expString = "String that the SpyBean should return.";
        // Replace the type in the ResultCaptor bellow from String to whatever your method returns.
        final Test.ResultCaptor<String> resultCaptor = new Test.ResultCaptor<>();
        doAnswer(resultCaptor).when(<replace_classToSpyOn>).<replace_methodOnSpyBean>(param_1, param_2);

        // When
        <replace_classUnderTest>.<replace_methodUnderTest>(param_1, param_2);

        // Then
        Assert.assertEquals("Error message when values don't match.", expString, resultCaptor.getResult());
    }
}

Now that this is out of the way .既然这样,那就不碍事了 There are situations where you would want to verify that your SpyBean is returning the result value.在某些情况下,您需要验证您的 SpyBean 是否正在返回结果值。 For example, there are two internal method invocations in your method under test that would produce the same value.例如,被测方法中有两个内部方法调用会产生相同的值。 Both get invoked but only one of them produces the wanted result.两者都被调用,但只有其中一个产生想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM