How do I use Mockito to mock a protected method?

This is not an issue with Mockito, but with plain old java. From where you are calling the method, you don’t have visibility. That is why it is a compile-time issue instead of a run-time issue.

A couple options:

  • declare your test in the same package as the mocked class
  • change the visibilty of the method if you can
  • create a local (inner) class that extends the mocked class, then mock this local class. Since the class would be local, you would have visibility to the method.

Leave a Comment