Skip to content

Starting from 3.2.x, @SpyBean is not able to initialise MongoRepository bean of the generic type #40234

@pavlo-reshetniak

Description

@pavlo-reshetniak

Issue

Starting from Spring-Boot 3.2.x the @SpyBean is not able to initialize MongoRepository bean of the generic type.
Please check the example project: https://github.com/pavlo-reshetniak/spybean-issue-example

How to reproduce

  1. Make sure the spring-boot-starter-parent has version 3.2.x (e.g. 3.2.4) in the parent POM file
  2. Run unit test: com.example.service.ExampleServiceTest#shouldExtractDataFromMongo

Result

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.repository.ExampleRepository]: Specified class is an interface	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:77)	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1311)	... 39 more 

Possible workaround

  • Downgrading the spring-boot-starter-parent to version 3.1.10 will fix the issue
  • Removing the generic type under the @SpyBean initialization will fix the issue:
From this: @SpyBean private ExampleRepository<ExampleData> exampleRepository; to this: @SpyBean private ExampleRepository exampleRepository; 

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions