youngDev
youngDev

Reputation: 339

Reactive repository does not save an object

I think I don't understand well how a Reactive repository and handlers using it work. I have written a special test class only to test the simpliest handler using a repository

 @SpringBootTest
 class TestRepository() {

   @Autowired
   lateinit var myRepo: myRepo

   @Autowired
   lateinit var myHandler: MyHandler

   @Test
    fun `save with a handler`() {
    val myObject = MyObject()
    myHandler.save(request).subscribe()

    StepVerifier.create (myRepository.count() ) <--this does not work
        .expectNext (1L )
        .expectComplete().verify()
   }

   @Test
   fun `test only database saving`() {
      val object = MyObject()

      myRepo.save(myRepo).subscribe()

      StepVerifier.create (myRepo.count() ) <-- this works
        .expectNext (1L )
        .expectComplete().verify()
   }
}

my handler and repository are defined in the following way:

  @Service
  class MyHandler(private val myRepository: MyRepository) {

     fun save(object: MyObject): Mono<MyObject> {
       return myRepository.save(request)
     }
  }

  @Repository
  interface MyRepo : ReactiveMongoRepository<MyObject, String> {

    fun save(request: MyObject): Mono<MyObject>
  }

I also tried to play with subscribe method but it still does not see the results.

What should I correct?

Upvotes: 1

Views: 769

Answers (1)

Ilya Zinkovich
Ilya Zinkovich

Reputation: 4410

Use Mono.then function to chain save and count functions and get a resulting Mono:

@Test
fun `save with a handler`() {
  val countAfterSave = myHandler.save(MyObject()).then(myRepository.count());

  StepVerifier.create(countAfterSave)
    .expectNext(1L)
    .expectComplete()
    .verify()
}

Upvotes: 2

Related Questions