Mocking final classes and methods is possible in modern Mockito through the inline mock maker, which uses a Java agent-based bytecode approach instead of the older subclassing mechanism.
Key Points: • Standard Mockito historically couldn't mock final classes/methods because its default mock maker relies on subclassing, which final prevents. • The mockito-inline module (available since Mockito 2.1.0) enables final mocking via ByteBuddy and an instrumentation agent. • Since Mockito 5, the inline mock maker is the default, so final mocking works out of the box without extra configuration. • Earlier versions (Mockito 1.x and early 2.x without the inline dependency) could not mock final types at all. • Frequent need to mock final classes may indicate a design that would benefit from depending on interfaces instead.
Example: Mocking a final class like an SDK's HttpClient wrapper that you can't modify becomes possible with the inline mock maker, whereas the classic subclass-based mock maker would fail with a "cannot mock final class" error.
Interview Tip: A concise interview answer is:
"Final classes and methods can be mocked using Mockito's inline mock maker, which instruments bytecode instead of subclassing, and it's the default since Mockito 5. Earlier Mockito versions without mockito-inline couldn't mock final types at all because the default maker relies on subclassing."