Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito avoid passing method arguments when calling Mockit.verify()

I want to test some method, for example:

      public class testObj {
        .. 
        public void upload(Context context, Data data, Info info, Listener listener, Bla bla, ....) {
          ...
        }

        }

now in some cases i just want to know that this method was called, but i do not care about anyy of the arguments passed.

Now calling Mockito.any(Foo.class) is very discouraging, I know i can also use matchers but it's not that great also.

Is there some cleaner way to achive this?

like image 508
oferiko Avatar asked Nov 27 '25 21:11

oferiko


1 Answers

No; verify needs to identify the method you're referring to, which means you'll need to call the correct method signature. Keeping an actual method call will also allow IDEs and automated refactoring tools to search for and modify the calls appropriately.

If you're running your tests from a Java 8 source environment, you can use any() with no argument; Java 8 has improved the ability to infer generic types when given as a parameter.


Though it usually makes more sense just to use matchers and explicit calls, you do have a few similar capabilities:

  • For stubbing, you can sometimes use a default answer to avoid specifying a lot of redundant calls and method values, but that won't help you with verification.
  • For verification, you can use MockingDetails.getInvocations() to inspect calls without using the built-in Mockito capabilities.
  • PowerMockito has private method verification by name, but not the same for public methods.
like image 128
Jeff Bowman Avatar answered Dec 02 '25 06:12

Jeff Bowman