如何使用 Kotlin 和 Kotest 正确单元测试 Spring Boot Reactive 应用程序

问题描述

我对 Kotlin 和 Kotest 还很陌生,一直在摸索如何正确地对 webflux 应用程序进行单元测试。查看 kotest 示例 here,对我来说,它看起来像是在启动 WebTestClient 服务器,更像是集成测试(如果我错了,请纠正我)。

我的应用程序相当简单,我有一个休息控制器,我正在使用构造函数注入来注入我的服务。

此服务使用 WebClient 调用返回 Mono<MyResponse> 的其他外部服务。我的测试看起来像这样:

@SpringBootTest
class MyControllerTest : FunSpec({
  lateinit var service: MyService
  lateinit var controller: MyController

  beforeTest {
    service = mockk()
    controller = MyController(service)
  }

  test("should return my response") {
     val myResponse = MyResponse(name = "John Doe")

     every { service.getName(any()) } returns Mono.just(myResponse)

     val response = controller.getName()
     
     verify { service.getName(any()) }
    
     response shouldBe myResponse
   }
})

我得到的错误是:

expected: MyResponse(name = "John Doe")
actual: Monojust

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...