You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I run this test, it seems that the @SpyBean annotation does not do its job, that should be proxying the ItemReader bean that's already present in the context, and so I obtain the (correct) exception because, as per definition, if the bean is not found it tries to instantiate a new bean of that type (and I have specified an interface) :
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.ItemReader]: Specified class is an interface
I'm pretty sure that the bean (of ItemReader type) is already in the context because :
debugging, I see that the target bean instantiation is correctly processed
if I change the @SpyBean annotation to an @Autowired annotation, the instance of previous point is correctly injected
@philwebb I've got it! The problem is the use of Generics.
If I use Generics
@SpyBean
private ItemReader<String> reader;
I get the exception, if I use
@SpyBean
private ItemReader reader;
the ApplicationContext load successfully and the bean is being spied on.
So it seems that the MockitoPostProcessor (or some sibling component) does not consider the generic type and so finish with trying to create a new instance of that type.
Core Spring instead, starting from version 4.0, consider the generic type when looking for the bean to inject.
changed the title [-]Spring boot @SpyBean tries to instantiate a new bean instead of spying the one in context[/-][+]@SpyBean does not work with generics[/+]on Dec 19, 2016
@yannanzheng Yes, we believe the problem was fixed in 1.4.3. If you have a problem in a later version please open a new issue with a small sample that reproduces the problem.
Activity
philwebb commentedon Dec 12, 2016
Could you please provide a sample application that replicates the issue?
lincetto commentedon Dec 12, 2016
Hi @philwebb, sure!
Here a repo with a sample test application
https://github.com/lincetto/spring-boot-ghissue-7625
lincetto commentedon Dec 12, 2016
@philwebb I've got it! The problem is the use of Generics.
If I use Generics
I get the exception, if I use
the ApplicationContext load successfully and the bean is being spied on.
So it seems that the MockitoPostProcessor (or some sibling component) does not consider the generic type and so finish with trying to create a new instance of that type.
Core Spring instead, starting from version 4.0, consider the generic type when looking for the bean to inject.
philwebb commentedon Dec 13, 2016
Thanks for the sample. The underlying cause does indeed seem to be the generics. I've raised Spring JIRA SPR-15011 to see if we can improve things.
jhoeller commentedon Dec 19, 2016
Since this is adressed in the core container in the meantime, does it work for Boot's purposes now?
[-]Spring boot @SpyBean tries to instantiate a new bean instead of spying the one in context[/-][+]@SpyBean does not work with generics[/+]Add test for @spybean for generic bean definition
philwebb commentedon Dec 20, 2016
@jhoeller All good, I've added a test. Thanks you!
yannanzheng commentedon Mar 13, 2017
Have you fixxed this issue? I come across it recently.
wilkinsona commentedon Mar 13, 2017
@yannanzheng Yes, we believe the problem was fixed in 1.4.3. If you have a problem in a later version please open a new issue with a small sample that reproduces the problem.
philwebb commentedon Mar 13, 2017
@yannanzheng Also, if you aren't using our managed dependencies, please make sure you're using at least Spring Framework 4.3.5.