【问题标题】:Mockito mocking same named method with similar signaturesMockito 模拟具有相似签名的相同命名方法
【发布时间】:2019-05-03 00:54:09
【问题描述】:

我有一个类,比如SimpleClass,它有两个具有相同名称和相同数量参数但参数类型不同的函数。现在我假设模拟它们的返回值应该是使用两个带有适当匹配器的 when 语句,但是当我尝试时我得到以下错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 此处检测到错误的参数匹配器:

-> 在 mocks.MockTest.whenMethodsHaveSimilarSignatures(MockTest.java:28) -> 在 mocks.MockTest.whenMethodsHaveSimilarSignatures(MockTest.java:28)

这是我正在尝试的示例:

public class SimpleClass {

    public boolean doWork(String value, String name) {
        return false;
    }

    public boolean doWork(Integer value, String name) {
        return true;
    }
}



@RunWith(MockitoJUnitRunner.class)
public class MockTest {

    private SimpleClass thing;

    @Before
    public void setup() {

        thing = new SimpleClass();
    }

    @Test
    public void whenMethodsHaveSimilarSignatures() {

        when(thing.doWork(anyString(), anyString())).thenReturn(true);
        when(thing.doWork(any(Integer.class), anyString())).thenReturn(false);

        assertThat(thing.doWork("one", "name")).isTrue();
        assertThat(thing.doWork(1, "name")).isFalse();
    }
}

虽然我不是 Mockito 的向导,但我已经使用它一段时间了,从未遇到过这个问题。想法?我正在使用 Mockito-Core v2.2.9

【问题讨论】:

    标签: java unit-testing testing mockito


    【解决方案1】:

    在存根重载方法时不应使用any(Object.class),因为StringInteger 都是Object 的子类,因此在存根期间指定特定参数。您也可以使用ArgumentMatchers

    when(thing.doWork(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(true);
    when(thing.doWork(ArgumentMatchers.any(Integer.class), anyString())).thenReturn(false);
    

    而且你不是在嘲笑SimpleClass

    @RunWith(MockitoJUnitRunner.class)
    public class MockTest {
    
    private SimpleClass thing = Mockito.mock(SimpleClass.Class);
    
    @Before
    public void setup() {
    
        MockitoAnnotations.initMocks(this);  // need to enable these annotations programmatically
    }
    
    @Test
    public void whenMethodsHaveSimilarSignatures() {
    
        when(thing.doWork(anyString(), anyString())).thenReturn(true);
        when(thing.doWork(any(Integer.class), anyString())).thenReturn(false);
    
     //or
    
       when(thing.doWork(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(true);
       when(thing.doWork(ArgumentMatchers.any(Integer.class), anyString())).thenReturn(false);
    
        assertThat(thing.doWork("one", "name")).isTrue();
        assertThat(thing.doWork(1, "name")).isFalse();
        }
     }
    

    【讨论】:

    • 我实际上尝试了许多类,我实际上也尝试了 Integer.class 并且结果相同。我更新了帖子以反映这一点,以减少混淆。
    • 很好地抓住了不模拟对象,显然我需要远离键盘。有趣的是,尽管我将库升级到 2.27 并使用了您的示例,但现在我根本无法模拟该类,得到一个异常 org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class com.salesforce.rtim.datatransfer.mocks.handlerMock.SimpleClass.
    • 降级并尝试,这可能是我的不同问题,或者请在帖子@ars265 中上传错误和堆栈跟踪
    • 降级似乎有效。我认为新的依赖项可能存在问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    相关资源
    最近更新 更多