This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Is anyone using Reactive MongoDB Template with Spring?

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to learn Spring using this book https://www.packtpub.com/application-development/learning-spring-boot-20-second-edition.
I've come to the part where they start using tests against the MongoDB database using the "findBy" methods in ReactiveCrudRepository.
The problem that I'm currently running into is that it appears as though the first thing that happens is the data is inserted into the collection and then the findby happens.
I end up with something like this shown on the log:
However, I do not want the record created and I want this test to fail.
This is my repository code ImageRepository.java Here is my test method:
It appears as though line 2 in my test method first populates the MongoDB with the invalid record and then the assert happens.

Can some please enlighten me as to what I'm doing incorrectly?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit hard to say without seeing the whole project, but it's probably not the findBy() call that causes the record to be created in the database, but something else in your project.

Spring Boot automatically configures and does a lot of things automatically, which is on one hand good because it makes things easy and you don't have to write a lot of code to make things work, but on the other hand it makes it difficult to understand exactly what happens when you run the application. One of the things Spring Boot can do is automatically pick up certain files to populate the database when you start the application. For example, if you're using a regular relational database, you can put a file named data.sql in src/main/resources and it will automatically be executed (see chapter 78 of the Spring Boot manual). Maybe a similar mechanism exists for MongoDB and you have a file somewhere that gets loaded into the database at startup.

If you want to know exactly what's happening at startup, you can start your Spring Boot application with the --debug option on the command line, which will cause Spring Boot to write a detailed auto-configuration report to the console, which is useful for finding out what exactly is happening.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your insights Jesper.

I'll see what I can do to figure this out. Most likely I do have something incorrect, hopefully it does not take too much more time to solve this.
However this is a good learning opportunity for me and possible others.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made some progress with this.

I was able to unwrap the Mono object, which turns it into a regular object so at is not the issue.
It appears that the issue that I'm experiencing is how StepVerifier is or isn't used/called.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the problem. In the book that I'm following they did not add ".expectComplete().verify()" to then end of the StepVefifier.

I found this bit of text

https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/StepVerifier wrote:Note that you must always call the verify() method or one of the shortcuts that combine the terminal expectation and verify,
like .verifyErrorMessage(String). Otherwise the StepVerifier won't subscribe to your sequence and nothing will be asserted.

On this web site https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/StepVerifier
Once I added ".expectComplete().verify()" to the end of the StepVerifier.create call things worked as expected.

Interesting that ".expectComplete().verify()" was supplied by the author of the book for the previous test, but not this test. This could be an errata for the book.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!