Mockito Stubbing Methods That Return Type With Bounded Wild-Cards
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Mockito is a widely used mocking framework for unit tests in Java applications. It allows developers to create mock objects, define their behavior, and specify interactions, thereby isolating the components under test. One of the features of Mockito is the ability to stub methods, which is essential for defining the output of method calls on mock objects. This article delves into the nuances of stubbing methods that return types with bounded wild-cards in Mockito.
Understanding Wild-Cards and Bounded Wild-Cards
Wild-Cards in Generics
Generics in Java introduce the concept of wild-cards, which are used to represent an unknown type. Wild-cards are specified using a question mark (?) and offer flexibility when working with parameterized classes and interfaces.
Bounded Wild-Cards
Bounded wild-cards restrict the types that a generic method or class can work with. Java supports two types of bounded wild-cards:
- Upper-bounded wild-cards: Represented by
<? extends T>, whereTis a certain type. It signifies that the unknown type is a subtype ofT. - Lower-bounded wild-cards: Represented by
<? super T>, indicating that the unknown type is a supertype ofT.
Stubbing Methods Returning Types with Bounded Wild-Cards
When using Mockito, stubbing methods that return bounded wild-cards can be slightly more complex than standard methods. Let's explore this with technical details and examples.
Example: Upper-Bounded Wild-Cards
Imagine a scenario where we have a generic class Container and a method that returns a list of items:
Here's how we can stub the getNumbers method using Mockito:
In this example, the getNumbers method is stubbed to return a list containing an Integer, a Double, and a Long. This exemplifies an upper-bounded wild-card where the return list can include any subtype of Number.
Example: Lower-Bounded Wild-Cards
Consider a situation with lower-bounded wild-cards:
Stubbing the method might differ because lower-bounded wild-cards are typically used as method parameters instead of return types. Here's how this would be interacted with in a test context:
In this case, the processItems method is configured to accept a list of Number instances, which is compatible with Integer.
Practical Tips and Key Points
- Type Safety: Generics and wild-cards in Java enforce compile-time type safety. Be sure to honor these constraints when stubbing methods.
- Mockito's Limitations: Mockito does not allow creating mocks of primitive types or final classes due to Java's type system and the limitations of proxies.
- Flexibility with Spy: If you need to stub part of a method's functionality while preserving the original behavior, consider using
Mockito.spy().
Summary Table
| Concept | Explanation |
| Wild-Cards | Represent unknown types in generics using ?. |
| Upper-Bounded Wild-Cards | <? extends T>: Represents a subtype of T. |
| Lower-Bounded Wild-Cards | <? super T>: Represents a supertype of T. |
| Stubbing Syntax | Mockito.when(...) for methods returning values,
Mockito.doNothing() for void methods. |
| Mocking Limitations | Cannot mock final classes or enums. |
Conclusion
Stubbing methods in Mockito that return types with bounded wild-cards is a powerful feature but requires a solid understanding of Java's generics system. This tutorial should provide a foundation for effectively using Mockito in scenarios involving such complex types. By leveraging the strengths of generics, developers create cleaner, more maintainable code. Keep exploring the rich feature set of Mockito to enhance your unit testing strategy.

