Update 15/Nov/2023

Since version 5.x, mockito switched its default mock runner to mockito-inline.

Mockito 5 switches the default mockmaker to mockito-inline, and now requires Java 11 (see Mockito GitHub homepage)

Use mockito 5.x, if you cannot, follow previous answer below:


As the error message suggests, you cannot mock final classes with the default Mockito.

But the community came up with mockito-inline, an extension bringing experimental features such as mocking final classes and methods or static methods.

Just add this in your pom.xml and use Mockito normally.

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <scope>test</scope>
</dependency>

and for Gradle:

testImplementation 'org.mockito:mockito-inline:4.11.0'
Answer from thchp on Stack Overflow
🌐
Davidvlijmincx
davidvlijmincx.com › home › testing › mockito mock record
Mockito mock record
January 19, 2023 - This article looks at how to mock a Record in Java. Mockito 5 made this very easy. Using Mockito 5 you can mock a Record as you would mock any other class.
🌐
GitHub
github.com › mockito › mockito › issues › 3107
Records: First mock of record with annotated parameter fails · Issue #3107 · mockito/mockito
August 31, 2023 - class TestMockito { @interface MyAnnotation{}; public record RecordA(@MyAnnotation Object o, Object other) {} public record RecordB(Object o, Object other) {} public record RecordC(@MyAnnotation Object o) {} public record RecordD(Object other, @MyAnnotation Object o) {} @Test public void testA_1() { var test = Mockito.mock(RecordA.class); } @Test public void testA_2() { var test = Mockito.mock(RecordA.class); } @Test public void testB() { var test = Mockito.mock(RecordB.class); } @Test public void testC() { var test = Mockito.mock(RecordC.class); } @Test public void testD() { var test = Mockito.mock(RecordD.class); } }
Author   infeo
🌐
javathinking
javathinking.com › blog › how-to-mock-a-record-with-mockito
How to Mock a Record Class with Mockito: Resolving the 'Cannot Mock Final Class' Error — javathinking.com
Isolating Tests: To isolate the ... record instances (e.g., null fields, invalid data). Mockito, by default, cannot mock final classes or methods....
🌐
Semaphore
semaphore.io › home › stubbing and mocking with mockito and junit
Stubbing and Mocking with Mockito and JUnit - Semaphore Tutorial
April 3, 2024 - Learn how to create true unit tests by mocking all external dependencies in your JUnit classes with the help of Mockito.
🌐
The Solution Space
solutionspace.blog › 2023 › 07 › 03 › escalating-java-records
Escalating Java Records – The Solution Space
July 3, 2023 - There is an easy fix for that: By configuring Mockito to use mock-maker-inline as described here, mocking will work as expected on final classes – and this includes records.
🌐
Vogella
vogella.com › tutorials › Mockito › article.html
Unit tests with Mockito - Tutorial
February 26, 2026 - Mockito greatly simplifies the development of tests for classes with external dependencies by providing clean, readable mocking syntax. A mock object is a dummy implementation for an interface or a class that simulates the behavior of real objects in a controlled way.
Find elsewhere
🌐
Baeldung
baeldung.com › home › testing › mock final classes and methods with mockito
Mock Final Classes and Methods with Mockito | Baeldung
August 13, 2025 - In this section, we’ll use the Mockito version 2.x or above to mock a final method and class.
🌐
HyperTest
hypertest.co › unit-testing › what-is-mockito-mocks-best-practices-and-examples
Best Practices for Using Mockito Mocks with Examples
June 3, 2024 - These limitations suggest that Mockito mocks are not complete without the mock object. For complex scenarios or when mocking final/static/private methods becomes a hurdle, consider alternative like HyperTest. HyperTest is a smart auto-mock generation testing tool that enables you to record real ...
🌐
Medium
medium.com › @alxkm › mocking-with-mockito-simplifying-unit-testing-in-java-1cc50d78d2c0
Mocking with Mockito: Simplifying Unit Testing in Java | by Alex Klimenko | Medium
August 9, 2025 - Not verifying interactions: Use verify(...) to ensure behavior. ... Mockito is a powerful, flexible tool that simplifies unit testing by allowing you to isolate dependencies and simulate behavior.
🌐
Mockito
site.mockito.org › javadoc › current › org › mockito › Mockito.html
Mockito (Mockito 2.2.7 API)
Original version of Mockito did not have this feature to promote simple mocking. For example, instead of iterators one could use Iterable or simply collections. Those offer natural ways of stubbing (e.g. using real collections). In rare scenarios stubbing consecutive calls could be useful, though: when(mock.someMethod("some arg")) .thenThrow(new RuntimeException()) .thenReturn("foo"); //First call: throws runtime exception: mock.someMethod("some arg"); //Second call: prints "foo" System.out.println(mock.someMethod("some arg")); //Any consecutive call: prints "foo" as well (last stubbing wins).
🌐
Mockito
site.mockito.org
Mockito framework site
// you can mock concrete classes, not only interfaces LinkedList mockedList = mock(LinkedList.class); // or even simpler with Mockito 4.10.0+ // LinkedList mockedList = mock(); // stubbing appears before the actual execution when(mockedList.get(0)).thenReturn("first"); // the following prints "first" System.out.println(mockedList.get(0)); // the following prints "null" because get(999) was not stubbed System.out.println(mockedList.get(999));
🌐
Javadoc.io
javadoc.io › doc › org.mockito › mockito-core › latest › org › mockito › Mockito.html
mockito-core 5.21.0 javadoc (org.mockito)
Bookmarks · Latest version of org.mockito:mockito-core · https://javadoc.io/doc/org.mockito/mockito-core · Current version 5.21.0 · https://javadoc.io/doc/org.mockito/mockito-core/5.21.0 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/org.mocki...
🌐
PriorityQueue
nkamphoa.com › home › how to mock file system with mockito
How to Mock File System With Mockito
April 6, 2024 - Sale.java is a simple Java record with two fields: public record Sale(String productCode, int saleAmount) { } Here is a snippet of the SalesProcessor#compute() method: Trending · How to Mock Static Methods With Mockito ·
🌐
CodingTechRoom
codingtechroom.com › question › how-to-mock-a-record-in-java-using-mockito
How to Mock a Record in Java Using Mockito - CodingTechRoom
Use the Mockito framework for mocking records by defining expected behavior with 'when()' and specifying return values. Ensure that you've added the Mockito dependency to your project. Utilize the '@Mock' annotation for more concise mock object creation.
🌐
Toptal
toptal.com › java › a-guide-to-everyday-mockito
A Unit Testing Practitioner’s Guide to Everyday Mockito | Toptal®
September 7, 2023 - A spy is the other type of test double that Mockito creates. In contrast to creating a mock, creating a spy requires an instance to spy on. By default, a spy delegates all method calls to the real object and records what method was called and with what parameters.
🌐
Medium
medium.com › @kaustubh.saha › simplifying-java-unit-testing-with-mockito-f4fa6e1ca2fb
Simplifying Java unit testing with Mockito | by Kaustubh Saha | Medium
November 20, 2025 - Mockito creates mock objects by generating dynamic proxies or subclass implementations at runtime, using bytecode manipulation libraries like CGLIB or ByteBuddy. These proxies intercept method calls, record them, and return stubbed responses ...
🌐
DZone
dzone.com › refcards › mockito
Mockito - DZone Refcards
In some rare cases it can be useful to implement a custom logic, later used on a stubbed method invocation. Mockito contains a generic Answer interface allowing the implementation of a callback method and providing access to invocation parameters (used arguments, a called method, and a mock instance).
🌐
Baeldung
baeldung.com › home › spring › mockito.mock() vs @mock vs @mockbean
Mockito.mock() vs @Mock vs @MockBean | Baeldung
December 3, 2024 - We don’t need to do anything else to this method before we can use it. We can use it to create mock class fields, as well as local mocks in a method. This annotation is a shorthand for the Mockito.mock() method. It’s important to note that we should only use it in a test class.