Anyone who has used Mockito for mocking and stubbing Java classes, probably is familiar with the InjectMocks-annotation. Use this annotation on your class under test and Mockito will try to inject mocks either by constructor injection, setter injection, or property injection. This magic succeeds, it fails silently or a
MockitoException is thrown.
I’d like to explain what causes the “MockitoException: Cannot instantiate @InjectMocks field named xxx! Cause: the type is an abstract class” and how to solve it.
Consider the following JUnit 5 test which verifies whether a waitress can properly serve breakfast. Anyone of the kitchen staff can serve breakfast, and the test verifies that when breakfast is served the coffee machine starts brewing coffee and the toaster starts toasting.
toaster are mocked by Mockito for the purpose of this test — hence they need the
Mock annotation — so we can verify if the expected methods are invoked. The waitress is the real deal, she is being tested. By putting
@InjectMocks on her, Mockito creates an instance and passes in both collaborators — and then our actual
@Test-annotated method is called.
Unfortunately it fails: as soon as you run the test, Mockito throws a runtime exception: “Cannot instantiate @InjectMocks field named ‘waitress’! Cause: the type ‘KitchenStaff’ is an abstract class.”
Luckily Mockito’s error messaging has improved lately and it cleary states what’s wrong: the type
KitchenStaff is an abstract class.
- We have an abstract class.12345678
- We say to Mockito: “instantiate this abstract class” (What?)12
- Hey, that can’t be right!
You can not use
@InjectMocks on just the abstract class alone, because Mockito needs to know what subclass to instantiate.
Remember that the unit you’re (unit) testing is one of the few lucky ones which usually are real. The
KitchenStaff is just too abstract, it knows how to hold on to a coffee machine and toaster, but the
Waitress actually knows how to operate them.
There are a few, just as with using interfaces, but it boils down to: provide a concrete subclass at instance declaration.
Give Mockito the subclass that extends the abstract parent class.
A) Declare a concrete subclass
Use a subclass for the type of the
B) Assign a concrete subclass
Keep using the parent’s type for the
@InjectMocks field, but initialize it with a concrete subclass.
Or of course use the subclass in the declaration and initialization, that works too 😉
However, does your class under test expects (required) collaborators as arguments to a constructor?
(I sure hope so!)
E.g. consider the following single constructor in the abstract class:
and the subclass needing to call the parent constructor:
Then, in the absence of a no-args constructor, the compiler would tell you to call the proper constructor and provide the arguments right there and now.
A. Solve it by providing the arguments yourself.
@InjectMocks. You don’t need it anymore.
The waitress is usually the only one who knows how to operate the coffee machine properly.
Published on Java Code Geeks with permission by Ted Vinke, partner at our JCG program. See the original article here: Mockito: Cannot instantiate @InjectMocks field: the type is an abstract class
Opinions expressed by Java Code Geeks contributors are their own.