How does Mockito handle overlapping matchers with multiple arguments in the thenReturn block

后端 未结 3 2662
醉梦人生
醉梦人生 2021-02-20 17:52

I\'ve got a block of test code that is attempting to, in the generic case return two values on subsequent calls, but in specific cases only return the value associated with that

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-20 18:17

    I had this problem today. It is caused by calls to the mock to set up stubbing actually consuming the stubbing already in place.

    In this example, change the first line to

    when(mock.call(anyString())).thenReturn("","",string1,string2)
    

    This will give you two blank responses when you set up your other mock returns, leaving string1 as the first useful return value.

    Try also the doReturn alternative which I think may not have these issues:

    doReturn(string1,string2).when(mock).call(anyString());
    

    This uses the stub differently during setup.

    So I did some more research on this. Here's the function I was playing with, based on the OP's question:

        Function function = mock(Function.class);
        when(function.apply(anyString())).thenReturn("A","B","C");
        when(function.apply("Jim")).thenReturn("Jim");
        when(function.apply("Bob")).thenReturn("Bob");
    
        assertThat(function.apply("Jim")).isEqualTo("Jim");
        assertThat(function.apply("Bob")).isEqualTo("Bob");
        assertThat(function.apply("")).isEqualTo("A");
        assertThat(function.apply("")).isEqualTo("B");
        assertThat(function.apply("")).isEqualTo("C");
        assertThat(function.apply("")).isEqualTo("C");
    

    The above fails at isEqualTo("A") because the two calls to set up the mocks for Jim and Bob consume return values from the list provided to anyString().

    You might be tempted to reorder the when clauses, but that fails, because the anyString() supersedes the special cases, so that fails too.

    The following version of the above DOES work as expected:

        when(function.apply(anyString())).thenReturn("A","B","C");
    
        doReturn("Jim")
            .when(function)
            .apply("Jim");
        doReturn("Bob")
            .when(function)
            .apply("Bob");
    
        assertThat(function.apply("Jim")).isEqualTo("Jim");
        assertThat(function.apply("Bob")).isEqualTo("Bob");
        assertThat(function.apply("")).isEqualTo("A");
        assertThat(function.apply("")).isEqualTo("B");
        assertThat(function.apply("")).isEqualTo("C");
        assertThat(function.apply("")).isEqualTo("C");
    

    This is because the doReturn technique, which is intended for modifying pre-existing mocks in flight, doesn't actually involve calling the method on the mock to set up the mocking.

    You could use doReturn for all setup, rather than mixing between when...thenReturn and doReturn..when..function(). As it happens, that's a bit uglier:

        doReturn("A").doReturn("B").doReturn("C")
            .when(function)
            .apply(anyString());
    

    There's no convenient varargs function to let you specify multiple returns in sequence. The above has been tested and does work, though.

提交回复
热议问题