jeudi 2 mai 2019

Mockito mocking same named method with similar signatures

I have a class, say SimpleClass, that has two functions with the same name and the same number of parameters but different parameter types. Now I assume mocking their return values should be as using two when statements with the appropriate matchers but instead when I attempt that I get the following error:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here:

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

Here's a sample of what I'm attempting:

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();
    }
}

While I'm not a wizard at Mockito I've been using it for a while and never encountered this issue. Thoughts? I'm using Mockito-Core v2.2.9

Aucun commentaire:

Enregistrer un commentaire