0

I'm fairly new to Kotlin and Kotest, been scratching my head on how to properly unit test webflux application. Looking over kotest samples here, to me that looks like it's spinning up a WebTestClient server and is more like integration test(please correct me if I'm wrong).

My app is fairly simple, I have a rest controller that I'm using constructor injecting to inject my service.

This service uses WebClient to call a different external service that returns a Mono<MyResponse>. And my test looks something like so:

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

The error I'm getting is:

expected: MyResponse(name = "John Doe")
actual: Monojust
Chris
  • 90
  • 10
  • You can call `block()` on the mono to get the value. Or there are other ways https://www.baeldung.com/java-string-from-mono – DCTID Feb 24 '21 at 02:46
  • @DCTID Thanks a bunch! Doing `response.block() shouldBe accounts` worked. Did some reading up on it. And I believe I'm going to go with `io.reactor.test` approach using `StepVerifier`. – Chris Feb 24 '21 at 05:33
  • I would recommend using the Kotlin extensions for the StepVerifier - then you will have a very fluent and nice way of testing the reactive methods. – Erik Finnman Feb 24 '21 at 14:47

0 Answers0