Skip to content

Lambda used as ArgumentMatcher causes decamelized lambda name to appear in error message #2061

@Marcono1234

Description

@Marcono1234

Version

Mockito version: 3.5.13

Description

Relates to #1932

When using a lambda or method reference expression as ArgumentMatcher and the matcher fails, the error message contains the lambda / method reference class name in "decamelized" form which is rather irritating, e.g.:

myInterface.doSomething(
<Mockito test$$ lambda$ 4 8/ 0x 0 0 0 0 0 0 0 8 0 0c 8f 4 4 0>
);

Expected would be that similar to anonymous classes the error output says "<custom argument matcher>". This could probably be achieved by testing for Class.isSynthetic() in org.mockito.internal.matchers.text.MatcherToString.toString(ArgumentMatcher<?>) before calling decamelizeMatcher there.

Test case

public class MockitoTest {
    interface MyInterface {
        void doSomething(String s);
    }
    
    private static boolean matches(String s) {
        return false;
    }
    
    public static void main(String[] args) {
        MyInterface mock = mock(MyInterface.class);
        try {
            verify(mock).doSomething(argThat(arg -> false));
        } catch (Error e) {
            e.printStackTrace();
        }
        try {
            verify(mock).doSomething(argThat(MockitoTest::matches));
        } catch (Error e) {
            e.printStackTrace();
        }
        
        // When an anonymous class is used the output is '<custom argument matcher>'
        // (as expected)
        verify(mock).doSomething(argThat(new ArgumentMatcher<>() {
            @Override
            public boolean matches(String argument) {
                return false;
            }
        }));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions