Getting @DataNeo4jTest to work in a project that also has a configuration for a real DB

Hi again @michael.simons1.
(@gerrit.meier you may also be interested / helpful?)

So, I've started a new Neo4j app using start.spring.io, and added the code from your Medium post about testing with TestContainers and @DataNeo4jTest.

The problem I have is that, with a "real" Neo4j configuration defined in the context of the @SpringBootApplication, the context of the test class won't startup because it's got a conflicting config.

The actual errors are:

java.lang.IllegalStateException: Failed to load ApplicationContext

and:

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'configuration' defined in com.grahamlea.neo4j_test_app.model.RepositoryTest$Config: Cannot register bean definition [SNIP] defined in com.grahamlea.neo4j_test_app.model.RepositoryTest$Config] for bean 'configuration': There is already [SNIP] defined in com.grahamlea.neo4j_test_app.GraphApiApplication] bound.

Earlier in the week I was playing around with it to try and get it working, I think by giving the beans different names in the @TestConfiguration, and I somehow got it set up so that data was being inserted into the test database but queries were going against my local Neo4j instance.

I've uploaded the code here if you want to have a look: https://github.com/GrahamLea/neo4j-test-app/

I'd really appreciate your help in getting it to work with one Neo4j config for the app to use and a different one for tests. My Spring test framework knowledge is a little rusty, so it's totally possible I'm doing something stupid that has a very simple fix.

Cheers,

Graham.

Hi Graham.

Sorry that you feel like you're entering a world full of pain. I assure you that's not the case.
Thanks for taking the time to share your setup with us.

The reasons for things falling apart are not that much related to Spring Data Neo4j. It would happen in similar ways with other Spring Data Stores.

I have send you our suggestions as Pull Request with detailed commit messages here:

Please have a close look at each message.

There's nothing I would change or add to the way @DataNeojTest works… It's according to the Spring specs.

As it happens I started to write a little Spring / Neo4j knowledge base: https://michael-simons.github.io/neo4j-sdn-ogm-tips/ogm_sdn_knowledge_base.html
I would highly recommend having a look at the presentions linked under Custom programmatic configuration for Neo4j-OGM?, for example my talk on the topic of automatic configuration in Spring Boot: https://www.youtube.com/watch?v=wSWA63rZfGo

Less explicit configuration is more in most cases with Boot.

The issues in your original code is that you have inadvertently worked around the automatic configuration and stuff broke in the long run.

I removed the manual config and reduced the test setup by pointing the configuration to the values applicable for the test.

So, I understand you are creating a new app and you are a Kotlin fan:
We are working on a fresh start with Spring Data Neo4j which is called SDN/RX for the time being. It has much better Kotlin and immutable data classes support than SDN/OGM. You'll find it here: https://github.com/neo4j/sdn-rx/

That's also the way you have to go if you want to use Springs reactive features or Kotlins Co-Routines.

Take care and have a nice weekend,
Michael

1 Like

Hey Michael.

Thanks so much for taking the time to help me sort this out. As I find is often the case with Spring, the problem was in not understanding the hidden magic. :grimacing: I'm only just starting to dip my toe in with Neo4j and SDN, so I'm still at the mercy of whatever examples I find on the web, which isn't helped by the fact that most of them use Neo4j 3 and/or JUnit 4. I probably need to invest some time in reading the user guide front to back.

Thanks also for the pointers to resources. I'll check them out. Am I right in understanding that SDN/RX is intended to eventually replace SDN/OGM?

Cheers,

Graham.

You're welcome Graham.
I wonder what the Spring Boot team in general and I in special could do to help this :/

Yes, SDN/RX will be replace SDN/OGM in the next major Spring Data release (end of year).