Using Mockito's generic “any()” method

后端 未结 4 1347
醉酒成梦
醉酒成梦 2020-12-04 09:40

I have an interface with a method that expects an array of Foo:

public interface IBar {
  void doStuff(Foo[] arr);
}

I am mock

4条回答
  •  抹茶落季
    2020-12-04 10:21

    As I needed to use this feature for my latest project (at one point we updated from 1.10.19), just to keep the users (that are already using the mockito-core version 2.1.0 or greater) up to date, the static methods from the above answers should be taken from ArgumentMatchers class:

    import static org.mockito.ArgumentMatchers.isA;
    import static org.mockito.ArgumentMatchers.any;
    

    Please keep this in mind if you are planning to keep your Mockito artefacts up to date as possibly starting from version 3, this class may no longer exist:

    As per 2.1.0 and above, Javadoc of org.mockito.Matchers states:

    Use org.mockito.ArgumentMatchers. This class is now deprecated in order to avoid a name clash with Hamcrest * org.hamcrest.Matchers class. This class will likely be removed in version 3.0.

    I have written a little article on mockito wildcards if you're up for further reading.

提交回复
热议问题