Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Marcono1234 opened this issue Oct 7, 2020 · 2 comments · Fixed by #2071
Closed

Comments

@Marcono1234
Copy link
Contributor

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;
            }
        }));
    }
}
@mockitoguy
Copy link
Member

This could probably be achieved by testing for Class.isSynthetic() in

Sounds like a good idea. Do you want to submit a PR? Thanks!

@Marcono1234
Copy link
Contributor Author

Sorry for the delay, have submitted the pull request: #2071
Feedback is appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants