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 OverflowDavidvlijmincx
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.
Videos
09:03
Mocking Final Classes and Methods - YouTube
14:59
Mocks, Stubs and Spies with Mockito - JUnit Tutorial - YouTube
10:46
Mock, When and ThenReturn with Mockito - JUnit Tutorial - YouTube
16:09
Tips, tricks and gotchas when writing unit tests with Mockito by ...
03:41:54
Complete JUnit & Mockito Tutorial Course: From Zero to Hero 2022 ...
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
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.
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 ...
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...
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.
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.