Mocking Final Classes and Methods
When you mock an interface, Mockito generates an implementation of that interface—not terribly surprising. But Mockito can mock classes too, which requires extending the class and overriding some or all of its methods.
What happens, though, when you ask Mockito to mock a method or a class marked final? Final methods can’t be overridden, and final classes can’t be extended, at least not without some help via reflection. What does Mockito do then?
Mock finals in Mockito 5 | |
---|---|
In this section, we discuss the additional configuration needed to mock final methods and classes. The ability to mock final members was added ... |
Get Mockito Made Clear now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.