Mockito: mock a generic class or interface
Codemia Knowledge Hub ·
To mock a class or interface with generic parameters in Mockito, call mock() on the raw class and assign the result to the parameterized type, as in mockedList = mock(List.class) with mockedList declared as a list of strings. You stub and verify it the same way as any other mock. Because of Java's type erasure, the type argument only exists at compile time, so keeping it consistent is up to you.
Generics and type erasure
Generics let classes, interfaces and methods work with different types while catching more bugs at compile time. Java's type erasure removes the generic type information during compilation, which is why you pass the raw class (such as List.class) to Mockito. Mockito can mock concrete classes, abstract classes and interfaces, generic or not.
Mocking a generic interface
The steps are the same as for non-generic types:
List is a generic interface, and it is mocked here without a type parameter. Assigning it to a parameterized variable is an unchecked conversion (the compiler may warn about it); at runtime the mock is simply a List.
Stubbing a method with a generic return type
Methods whose return type is a type parameter look trickier. Take this interface:
Mock and stub it like this:
Mockito mocks Service<T> and stubs process(T data) without any special handling. Make sure the generic types on the mock declaration and in the stubbing line up, because the compiler only checks them against the declared type.
The check uses an explicit throw rather than Java's assert, which does nothing unless the JVM runs with -ea. In a real test, use your framework's assertEquals.
Avoiding the unchecked warning
mock(Service.class) compiles with an unchecked conversion warning. Two ways to avoid it:
What Mockito cannot see
Due to type erasure, Mockito cannot tell List<String> from List<Integer> at runtime. Careful design and clear test scope usually work around this.
| Feature | Consideration |
| Generic Interfaces/Classes | Can be mocked directly but watch for type erasure. |
| Generic Methods | Ensure correct usage of generic types in stubbing. |
| Type Safety | Managed by the developer through the declared generic types. |
| Limitations due to Type Erasure | Mockito cannot differentiate between parameterized types at runtime. |
Related reading
- Using Mockito to test abstract classes
- Using Mockito's generic any method
- Using multiple WebSecurityConfigurerAdapter in spring boot
- Using NotNull Annotation in method argument
- Using Moq to determine if a method is called
- Using Moq to mock an asynchronous method for a unit test
- Using Pairs or 2-tuples in Java
- Using RabbitMQ (Java client), is there a way to determine if network connection is closed during consume?
Course lessons on this
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.