Reputation: 59
I updated my project with the latest revision of Spring (2.0.0.RELEASE) and while my tests worked in 2.0.0.RC1, now it doesn't work and it keeps giving me this error :
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'service'! Cause: the type 'PersonService' is an interface.
You haven't provided the instance at field declaration so I tried to construct the instance.
Examples of correct usage of @InjectMocks:
@InjectMocks Service service = new Service();
@InjectMocks Service service;
//and... don't forget about some @Mocks for injection :)
Here I made a minimal project where you can change the version in the pom file to see it succeed on 2.0.0.RC1 and fail in 2.0.0.RELEASE.
For the a full minimal test - please turn to gituhub.
Upvotes: 2
Views: 1029
Reputation: 1750
From the documentation for the @InjectMocks
Mockito cannot instantiate inner classes, local classes, abstract classes and of course interfaces.
In your case you should use inside your test:
@InjectMocks
private PersonServiceImpl underTest;
I have checked in your sample from github, if you change to the implementation of service - tests will be passed
Upvotes: 1