Home > Net >  @SpringBootTest loads unrequired Bean when making IT
@SpringBootTest loads unrequired Bean when making IT

Time:04-07

I'm making some Integration Tests for my app and I'm encountering this problem I can't see how to solve.

I'm using Spring Boot 2.4.13 Spring Data Neo4J 6.1.9

FYI, I deleted the Application default test that comes bundled when you create a project through Spring Initializr, and under /src/test/resources I have a .yml file named application.yml

My IT class looks like this:

@SpringBootTest
public class ClientIT {

    @Autowired
    private ClientServiceImpl service;

    @Autowired
    private ClientRepository repository;

    @Test
    void someTest() {
       //Given
       //When
       //Then
    }
}

But when I run this test I get the following Exception:

java.lang.IllegalStateException: Failed to load ApplicationContext

And this is the cause:

Caused by: java.lang.IllegalStateException: The provided database selection provider differs from the ReactiveNeo4jClient's one.

The thing is I don't use SDN's Reactive features at all in my project. I don't even understand why Spring tries to load it. I've created an Issue under the Spring Data Neo4j GitHub repository (https://github.com/spring-projects/spring-data-neo4j/issues/2488) but they could only tell me that ReactiveNeo4jDataAutoConfiguration gets automatically included if there's a Driver or Flux class in the classpath which I don't have.

I've been debugging the Spring internals while booting up the Application after JUnit Jupiter methods to no success.

What I could see is that at some point after JUnit Jupiter tests preparation/initialization, "reactiveNeo4jTemplate" gets injected into DefaultListableBeanFactory's beanDefinitionNames variable.

I've tried many combinations of different annotations intended to be used when making Integration Tests but the one time it worked was after I explicitly excluded ReactiveNeo4jDataAutoConfiguration class through

@EnableAutoConfiguration(exclude=ReactiveNeo4jDataAutoConfiguration.class)

What I've always seen in some blogposts is that by using @SpringBootTest I shouldn't worry about this kind of problem but it looks like I need to add that annotation every time I want to make a new IT test.

My Integration Tests basically consist of bootstrapping the application web server (tomcat) along with an embedded Neo4J instance and after that, making requests to check everything works as it should. Do I really need to worry about all of this just to make these simple tests?

Thank you

References:

How do I set up a Spring Data Neo4j integration test with JUnit 5 (in Kotlin)?

SprintBootTest - create only necessary beans

CodePudding user response:

Answering my own question after finding what is causing this error:

In the linked Github Issue, one of the developers says having Flux.class in the classpath forces SDN to instantiate Neo4jReactiveDataAutoConfiguration which is what is causing the other reactive beans to instantiate.

Apparently, neo4j-harness brings io.projectreactor (where Flux.class belongs) as an indirect dependency through neo4j-fabric which is the root of our problems.

The Spring Data Neo4j will be fixing this issue in a patch later this week.

  • Related