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

@InjectMocks and @Spy on same field should cause MockitoException #169

Closed
makasprzak opened this issue Feb 15, 2015 · 3 comments
Closed
Labels

Comments

@makasprzak
Copy link

Some time ago there was @InjectMocks and @Spy cannot be used together when object initialized by mockito issue rejected.
Checking the codebase I found a test case WrongSetOfAnnotationsTest.shouldNotAllowSpyAndInjectMock() which, I guess, was supposed to cover Brice's explanation. The test passes because the MockitoException is thrown, but it's thrown for different reason: the test tries to instantiate an interface (List). The test was there at list since 2010, so I think the behavior could have been basically forgotten.
So, although I find Brice's explanation convincing, I still think the exception should be thrown in that case, informing of wrong Mockito usage.

makasprzak added a commit to makasprzak/mockito that referenced this issue Feb 15, 2015
@makasprzak
Copy link
Author

Now seeing the other thest: MockInjectionUsingSetterOrPropertyTest, I got a bit confused as it suggests this combination is ok (only the injection of such created spy will not happen), which seems to be in conflict with WrongSetOfAnnotationsTest.shouldNotAllowSpyAndInjectMock(). Is it an obsolete test then?

@bric3
Copy link
Contributor

bric3 commented Feb 16, 2015

Hi thanks a lot for spotting this.

So yes this is the combination of @InjectMocks and @Spy an undocumented feature, understand not promoted feature. The test is indeed wrong as the cause because List is an interface.

The combination of Spy and InjectMocks is not to be removed in the foreseeable future, however the issue at hand in 489 is about managing multiple @InjectMocks in the same unit test, which is the beginning of a DI framework as I explained it the issue page or in the mailing list on the same topic.

@bric3
Copy link
Contributor

bric3 commented Feb 16, 2015

I have fixed the wrong unit test. Again thanks a lot for taking a look on the matter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants