Stubbing static methods with PowerMock

The current recipe will be about stubbing a static method in order to properly verify the behavior of the system under test. Unfortunately, Mockito can't stub static methods, and that's why we will use PowerMock to do that.

I'd like to yet again remind you that it absolutely isn't good practice to use PowerMock in your well-written code. If you follow all of the SOLID principles (please refer to Chapter 2, Creating Mocks, for the explanation of each of these principles), then you should not resort to stubbing static methods. PowerMock can come in hand when dealing with legacy code or stubbing third-party libraries (you can check Chapter 8, Refactoring with Mockito, to see how to use PowerMock to refactor the ...

Get Mockito Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.