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

后端 未结 3 2693
醉梦人生
醉梦人生 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:16

    One way to workaround this problem could be to use a regular expression to avoid overlapping as next:

    when(mockObject.method(eq("expectedInput1"))).thenReturn(string1);
    when(mockObject.method(eq("expectedInput2"))).thenReturn(string2);
    // Match with any input string that doesn't contain expectedInput1 neither expectedInput2
    when(mockObject.method(matches("((?!expectedInput1|expectedInput2).)*")))
        .thenReturn(string1, string2);
    

    Example:

    System.out.println("expectedInput1=" + mockObject.method("expectedInput1"));
    System.out.println("expectedInput2=" + mockObject.method("expectedInput2"));
    System.out.println("foo=" + mockObject.method("foo"));
    System.out.println("bar=" + mockObject.method("bar"));
    System.out.println("bar=" + mockObject.method("bar"));
    

    Output:

    expectedInput1=string1
    expectedInput2=string2
    foo=string1
    bar=string2
    bar=string2
    

    Another way could be to implement your ArgumentMatcher still to avoid overlapping:

    when(mockObject.method(eq("expectedInput1"))).thenReturn(string1);
    when(mockObject.method(eq("expectedInput2"))).thenReturn(string2);
    when(
        mockObject.method(
            argThat(
                new ArgumentMatcher<String>(){
                    @Override
                    public boolean matches(final Object argument) {
                        return !"expectedInput1".equals(argument) 
                            && !"expectedInput2".equals(argument);
                    }
                }
            )
        )
    ).thenReturn(string1, string2);
    

    Another way could be to implement your Answer with something like this:

    when(mockObject.method(anyString())).thenAnswer(
        new Answer<String>() {
            Iterator<String> it = Arrays.asList(string1, string2).iterator();
            String result;
            @Override
            public String answer(final InvocationOnMock invocation) throws Throwable {
                String argument = (String) invocation.getArguments()[0];
                switch (argument) {
                    case "expectedInput1" :
                        return string1;
                    case "expectedInput2" :
                        return string2;
                    default:
                        if (it.hasNext()) {
                            result = it.next();
                        }
                        return result;
                }
            }
        }
    );
    
    0 讨论(0)
  • 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<String, String> 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.

    0 讨论(0)
  • 2021-02-20 18:39

    It's hard to say whether that's a bug or a feature... The thing is, when you call mockObject.method(eq("expectedInput1")) to perform the second stubbing, the first stubbing is already in place. So this call returns string1, which is then uselessly passed to when. Subsequent calls return string2, and that includes the call for the last stubbing and later calls during actual testing.

    I can hardly see any elegant way around it, short of using a custom Answer like @Nicolas suggested, although it does seem like an overkill. You could, perhaps, use a custom matcher instead of anyString(), that would essentially say “any string except those two”. This way you won't have one matcher intersect with another.

    P. S. Now that @Nicolas edited his answer, that regular expression looks like exactly what I meant. Except that you don't need to implement a custom matcher after all.

    0 讨论(0)
提交回复
热议问题