简体   繁体   English

验证多态方法

[英]mockito verifying polymorphic methods

I'm trying to verify call to a polymorphic method using mockito, and am confused about what the best way forward is. 我正在尝试使用Mockito验证对多态方法的调用,并且对前进的最佳方式感到困惑。

Example Class 示例类

public class Library {
    public boolean foo() {
        return true;
    }
    public boolean foo(String s) {
        return true;
    }
    public boolean foo(Integer s) {
        return true;
    }
}

The test class using mockito(ignore the fact that in LibraryTest class, Library is not the class-under-test, but rather I'm mocking it) 使用嘲笑的测试类(忽略在LibraryTest类中, Library不是被测类,而是我在嘲笑这一事实)

import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class LibraryTest {
    @Test public void testFoo1() {
        Library mockLibrary = mock(Library.class);
        mockLibrary.foo("Hi");
        verify(mockLibrary).foo(any());
    }
    @Test public void testFoo2() {
        Library mockLibrary = mock(Library.class);
        verify(mockLibrary, never()).foo(any());
    }
}

So both the verify statements don't compile, with the error "The method foo(String) is ambiguous for the type Library". 因此,两个verify语句均无法编译,并出现错误“方法foo(String)对于库类型是不明确的”。

The error kind of makes sense, any() tries to return a captor based on the type of the argument but the argument could be Integer or String or void . 错误类型是有道理的, any()尝试根据参数的类型返回捕获器,但参数可以是IntegerStringvoid

What I want to achieve is that in both the tests, a call to any of the foo methods is counted by the verify . 我要实现的是,在两个测试中,对任何foo方法的调用都由verify计数。 In other words the first verify call should succeed if I called any of the foo methods and the second verify should fail if I call any of the foo methods. 换句话说,第一个verify调用应该会成功,如果我叫任何foo方法和第二verify ,如果我调用任何失败了foo的方法。

Is there a way to make this happen? 有没有办法做到这一点?

You can use isA matcher 您可以使用isA匹配器

verify(mockLibrary).foo(isA(Integer.class));

verify(mockLibrary).foo(isA(String.class));

and btw use Mockito.spy instead of Mockito.mock when you only want to see if some methods have been called on the class under test 当您只想查看被测类是否已调用某些方法时,btw使用Mockito.spy而不是Mockito.mock

EDIT with example (written in few minutes, don't mind the code:)) based on the new op details. 根据新的操作细节,使用示例进行编辑(几分钟后编写,不用管代码:)。

public static class Library {
    public boolean foo() {
        return true;
    }

    public boolean foo(String s) {
        return true;
    }

    public boolean foo(Integer s) {
        return true;
    }

    public String x(){
        return "";
    }

    public void y(){
        return;
    }
}


public static class ResponseProvider {
    public boolean result;
}

@Test
public void testFoo1() {
    final ResponseProvider provider = new ResponseProvider();
    provider.result = false;
    Library lib = Mockito.mock(Library.class, new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if ((invocation.getMethod().getName().equals("foo"))) {
                provider.result = true;
                return true;
            }
            return invocation.callRealMethod();
        }
    });

    //lib.foo();
    //lib.x();
    //lib.y();
    assertTrue(provider.result);

}

First of all, You are mocking the same class which you are testing. 首先,您要模拟正在测试的同一类。 Thats not advisable but still 那不建议,但是仍然

Its giving this exception because mockito doesn't know which method you are verifying among the foo methods. 它给出了此异常,因为在foo方法中,mockito不知道您要验证哪个方法。 If you want to make sure the method you invoked is called with the right value, you can either use isA(ClassName) matcher or use can use ArgumentCaptor. 如果要确保使用正确的值调用调用的方法,则可以使用isA(ClassName)匹配器,也可以使用ArgumentCaptor。

Example of ArgumentCaptor ArgumentCaptor的示例

@Test public void testFoo1() {
    Library mockLibrary = mock(Library.class);
    mockLibrary.foo("Hi");
    ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);

    verify(mockLibrary).foo(stringCaptor.capture());
    String actualArgument = stringCaptor.getValue();

    assertEquals(actualArgument, "Hi");
}

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

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