Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when @InjectMocks is uses on an interface or enum field #538

Closed
ChristianSchwarz opened this issue Aug 9, 2016 · 0 comments

Comments

@ChristianSchwarz
Copy link
Contributor

By accident a tests delares a field of an interface type instead of the implementing class.

@InjectMocks
public InterfaceType unitUnderTest;

Injection is not possible on interfaces or enums. The error message should make this clear, the currenta message that doesn't help much:

org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'configurationManager'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However, I failed because: the type 'ConfigurationManager' is an interface.
Examples of correct usage of @InjectMocks:
   @InjectMocks Service service = new Service();
   @InjectMocks Service service;
   //also, don't forget about MockitoAnnotations.initMocks();
   //and... don't forget about some @Mocks for injection :)

The error message should be someting like:
The field 'unitUnderTest' can not be annotated with @InjectMocks cause the type 'InterfaceType ' not a class!

ChristianSchwarz pushed a commit to ChristianSchwarz/mockito-lambda that referenced this issue Aug 12, 2016
ChristianSchwarz pushed a commit to ChristianSchwarz/mockito-lambda that referenced this issue Aug 14, 2016
bric3 added a commit that referenced this issue Aug 15, 2016
Fixed #538 changed error message in case initialization for mock injection fails.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant