Skip to content

@SpyBean does not work with generics #7625

Closed

Description

@lincetto

I'm trying to unit-testing a Spring batch job inside Spring boot using JUnit.
I wrote this test class where I want to spy the bean ItemReader:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
@ActiveProfiles({"dev", "batch", "test-jobs"})
public class BatchJobTest {

	@Autowired
	private JobLauncherTestUtils jobLauncherTestUtils;
    	
	@Autowired 
	@Qualifier("contactDownloadAckJob")
	private  Job contactDownloadAckTaskJob;

	@SpyBean
	private ItemReader<CrsOscContact> reader;

	@Test
	public void testJob() throws Exception {
		given(this.reader.read()).willReturn(new CrsOscContact());
		//... blah blah blah
	}	
}

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

Activity

  1. added
    status: waiting-for-triage
    on Dec 12, 2016
  2. philwebb commented on Dec 12, 2016

    @philwebb
    Member

    Could you please provide a sample application that replicates the issue?

  3. added
    status: waiting-for-feedback
    and removed
    status: waiting-for-triage
    on Dec 12, 2016
  4. lincetto commented on Dec 12, 2016

    @lincetto
    Author
  5. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Dec 12, 2016
  6. lincetto commented on Dec 12, 2016

    @lincetto
    Author

    @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.

  7. added
    type: bug
    and removed
    status: feedback-provided
    on Dec 13, 2016
  8. added this to the 1.4.3 milestone on Dec 13, 2016
  9. philwebb commented on Dec 13, 2016

    @philwebb
    Member

    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.

  10. jhoeller commented on Dec 19, 2016

    @jhoeller

    Since this is adressed in the core container in the meantime, does it work for Boot's purposes now?

  11. 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
  12. added a commit that references this issue on Dec 20, 2016
    87547f2
  13. philwebb commented on Dec 20, 2016

    @philwebb
    Member

    @jhoeller All good, I've added a test. Thanks you!

  14. yannanzheng commented on Mar 13, 2017

    @yannanzheng

    Have you fixxed this issue? I come across it recently.

  15. wilkinsona commented on Mar 13, 2017

    @wilkinsona
    Member

    @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.

  16. philwebb commented on Mar 13, 2017

    @philwebb
    Member

    @yannanzheng Also, if you aren't using our managed dependencies, please make sure you're using at least Spring Framework 4.3.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions