Mocking Static Methods

Until Java 8, you couldn’t have static methods in interfaces at all. As for classes, static methods are associated with the class as a whole, rather than an instance, and they can’t be overridden. So what does it even mean to mock a static method?

It’s easy enough to imagine a system that has a dependency on a static method. For example, consider a class, called BioService, which takes a vararg list of strings in the constructor and grabs a summary of their associated pages at Wikipedia. Wikipedia maintains a MediaWiki Action API,[19] which you can use to access Wikipedia programmatically.

(All the URLs end with .php, which tells you how Wikipedia is implemented.)

The book’s GitHub repository contains a package called ...

Get Mockito Made Clear 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.