【问题标题】:Reactive repository does not save an object反应式存储库不保存对象
【发布时间】:2019-09-25 23:59:29
【问题描述】:

我想我不太了解反应式存储库和使用它的处理程序是如何工作的。我编写了一个特殊的测试类,只是为了使用存储库测试最简单的处理程序

 @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()
   }
}

我的处理程序和存储库按以下方式定义:

  @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>
  }

我也尝试过使用subscribe 方法,但仍然看不到结果。

我应该纠正什么?

【问题讨论】:

    标签: mongodb spring-boot kotlin reactive-programming spring-repositories


    【解决方案1】:

    使用Mono.then 函数链接savecount 函数并获得生成的Mono:

    @Test
    fun `save with a handler`() {
      val countAfterSave = myHandler.save(MyObject()).then(myRepository.count());
    
      StepVerifier.create(countAfterSave)
        .expectNext(1L)
        .expectComplete()
        .verify()
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-17
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2020-12-03
      • 2019-02-28
      • 1970-01-01
      • 2011-12-29
      相关资源
      最近更新 更多