简体   繁体   中英

Mockito ArgumentMatcher lambda function with parameter not recognizing method

I'm trying to learn Mockito framework and implementing some mocks. Currently, I want to use ArgumentMatcher to check if the passed object matches. I saw that in the past this was possible by creating a new class that extends ArgumentMatcher . However, now it's possible to use Java 8 lambda functions . This is how I'm trying to implement it:

private ArgumentMatcher<User> matchUser(User user) {
    return u -> u != null && user.getId() == u.getId();
}

IntelliJ is giving me an error, as it's telling that u doesn't have the getId method. Should't u type be inferred to User ?

You have Mockito version 1.x, where ArgumentMatcher is an abstract class. As of Mockito 2.x, they changed it to be an interface , allowing to use lambdas.

The source code of ArgumentMatcher shows that:

If you fix your dependencies to a more recent version of Mockito, it'll work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM