Starting from Spring-Boot 3.2.x the @SpyBean is not able to initialise MongoRepository bean of the generic type.
- Make sure the spring-boot-starter-parent has version 3.2.x (e.g. 3.2.4) in the parent POM file
- Run unit test: com.example.service.ExampleServiceTest#shouldExtractDataFromMongo
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
- Downgrading the spring-boot-starter-parent to version 3.1.10 will fix the issue
- Removing the generic type in under @SpyBean initialization will fix the issue:
From this:
@SpyBean
private ExampleRepository<ExampleData> exampleRepository;
to this:
@SpyBean
private ExampleRepository exampleRepository;