tl;dr:

With Android P a developer can mock final classes and stub final and static methods. This will allow tests to directly run against classes that were previously unmockable.

About Mockito

Mockito is a popular mocking library for Java. A developer can use it to intercept method calls and replace them with alternate behavior:

Such a method behavior replacement is referred to as “stubbing a method”.

Further the Mockito method spy creates stubbable clones of the original objects. The fields of the spy-objects are copies of the original’s fields and the methods of the spy-object can be stubbed:

Using Mockito’s plugin system certain components of Mockito can be replaced. E.g. mockito-android and dexmaker-mockito replace the regular mock-generation component with one capable of generating mocks that work on Android devices. Let’s use the term “Mockito variant” to talk about Mockito libraries with replaced components.

All variants support the same API, i.e. mock , spy , when , verify etc… .

Stubbing final methods

Regular Mockito mocks and spies are implemented as subclasses of the mocked classes. The all intercepting code lives in methods that override the original methods. Hence initially only non-final instance methods could be stubbed.

Since Mockito 2.1.0 the Mockito variant mockito-inline allows a developer to stub final methods too:

As final methods can’t be overridden the mock-generation code of mockito-inline modifies the byte code of the mocked class and inlines the intercepting code directly into the original methods.