https://kotlinlang.org logo
#server
Title
# server
o

Oleg Shuliak

11/02/2023, 1:32 PM
hey everyone. not sure if it’s the correct channel, but redirect me, please, if that’s not the right place to ask those questions I use webflux webclient and trying to test it work
Mockk
library. my call has
awaitBody<>
Copy code
withContext(requestContext.coroutineContext) {
            monolithClient.get("$PATH?company_id=${requestContext.getCompanyId()}")
                .retrieve()
                .onStatus(HttpStatus::isError) { response ->   
                   return@onStatus response.bodyToMono(String::class.java).map {
                        log(ERROR_MESSAGE, response.statusCode(), it)
                        RuntimeException("$ERROR_MESSAGE: $it")
                    }
                }
                .awaitBody<DepartmentResponse>()
        }
Trying to mock that response with the setup like this:
Copy code
val client: MonolithClient = mockk()
        val requestSpec: WebClient.RequestHeadersSpec<*> = mockk()
        val responseSpec: WebClient.ResponseSpec = mockk()
        val expectedResponse = //some data

        every { client.get(capture(pathSlot)) } returns requestSpec
        every { requestSpec.retrieve() } returns responseSpec
        coEvery {
            responseSpec.onStatus(any(), any()).awaitBody<DepartmentResponse>()
        } returns expectedResponse
leads the test to freeze and the coroutine is never finishing on
awaitBody
changing the implementation of the client to use
bodyToMono
helps with the test, but I would really like to understand how to manage mocking calls with
awaitBody
Any tips are appreciated 🙏
v

Viorel Ștefan Alexandrescu

11/08/2023, 10:01 AM
My gut feeling tells me you need to use a relaxed mock. Have a look here.
o

Oleg Shuliak

11/08/2023, 1:53 PM
tried - didn’t work out. even tried with
@Mockk
annotation - still the same