How to mock a final class with mockito
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When testing Java applications, one often encounters scenarios where interaction with external dependencies might need to be simulated. In many cases, this means mocking classes to ensure tests are fast and dependable. Mockito is one of the most popular frameworks used for this purpose, due to its straightforward and readable syntax and extensive features. However, mocking final classes and methods with Mockito requires a bit of special handling.
Understanding final in Java
In Java, the final keyword can be applied to classes, methods, and variables. When a class is marked as final, it cannot be subclassed. This presents a particular situation in unit testing because mocking frameworks like Mockito traditionally work by creating a subclass of the class to be mocked.
Initial Challenges with Mockito
Earlier versions of Mockito (before 2.x) did not allow mocking of final classes and methods directly. This limitation required developers to use workarounds or opt for other tools when faced with final systems. However, with the introduction of Mockito 2, capabilities for mocking final classes and methods were integrated, albeit they need to be explicitly enabled.
Enabling Mocking of Final Classes and Methods
To start mocking final classes and methods with Mockito 2.x and later, you need to add some configuration. The setup involves creating a file named org.mockito.plugins.MockMaker and placing it in the resources/mockito-extensions directory of your application. The file should contain one line:
This configuration essentially switches Mockito to an alternative mechanism (inline-mocking) that doesn't rely on subclassing but rather instruments the classes directly.
Mocking a Final Class: An Example
Consider a scenario where you have a final class that you want to mock in your unit testing:
To write a test for a class that integrates with PaymentProcessor, you would do something like:
In this example, PaymentProcessor is a final class. The test uses Mockito to mock it and stipulates the desired behavior, which is then used in a test assertion.
Considerations and Best Practices
While mocking final classes is supported, it’s essential to use this capability judiciously:
- Design Implications: The need to mock final classes might indicate design decisions that could be reevaluated. Consider whether high coupling and limited flexibility in the design necessitate the use of final classes.
- Testing Philosophy: Mocking should not replace good design practices. Dependence on extensive mocking might also signify the need for refactoring to simpler, more testable designs.
- Performance: Be mindful that the use of inline-mocking can affect the performance of your tests. In scenarios with extensive mocking requirements, this might become noticeable.
Summary Table
| Feature | Requirement / Action | Purpose |
| Mocking final classes or methods | Enable inline-mocking | Allows Mockito to mock classes and methods declared as final. |
| Mocking regular classes | Standard Mockito setup | Used for classes and methods not declared as final. |
| Performance considerations | Evaluate impact | Inline-mocking can slow down test execution times. |
| Testing philosophy | Reflect on necessity | Extensive mocking might indicate need for design improvements. |
In conclusion, Mockito’s capability to mock final classes is a valuable tool in the arsenal of a modern Java developer, yet it comes with considerations that should be carefully evaluated within the context of your project’s testing and design practices.
Related reading
- How to mock JWT authentication in a Spring Boot Unit Test?
- How to mock void methods with Mockito
- How to modify JsonNode in Java?
- How to modify request body before reaching controller in spring boot
- How to mock an import
- How to mock AWS DynamoDB service?
- How to negate a method reference predicate
- How to obtain all subsequence combinations of a String in Java, or C etc

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.