Mocking Spring’s reactive WebClient in Kotlin, using MockK for UnitTests

Daniel Bunte
1 min readJan 24, 2022

This is purely a quick Note To Self, so that I can find it again, when googling for it 🤓

Let’s assume we want to test a method, invoking a WebClient call in a Unit Test (no Integration Test). Example:

private fun loadSomething(id: String): SomeThing {
return runBlocking(Dispatchers.IO) {
webClient
.get()
.uri("/someurl/{id}", id)
.exchangeToMono { response ->
if (response.statusCode() == HttpStatus.OK) {…

--

--

Daniel Bunte

Leader, self-taught developer with interests in Architecture/Automation/Security.