Creating mocks of final classes with PowerMock

Although these situations should not take place in a well-written and test-driven system, there are cases in which it is necessary to mock some legacy code or third-party libraries that are impossible to be mocked only by means of Mockito. In this recipe, we will see how to deal with those abnormal situations using the PowerMock library. Remember, however, that this tool is extremely powerful and the very need to use it suggests that something may really be wrong with your code. The best outcome of using this library would be to use it as means to refactor the bad code and, at the end of the day, remove the PowerMock dependency from the system since it is no longer needed.

Getting ready

In order to ...

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.