Skip to content

Instantly share code, notes, and snippets.

@nosix nosix/ExampleTest.kt
Created Jul 12, 2018

What would you like to do?
pass JSONCompareMode to JSONAssert when testing with WebTestClient
class ExampleTests {
private lateinit var client: WebTestClient
fun test_of_get() {
"id": 1,
"description": "write webflux blog"
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import org.springframework.test.web.reactive.server.EntityExchangeResult
import org.springframework.test.web.reactive.server.WebTestClient
import java.nio.charset.StandardCharsets.UTF_8
import java.util.function.Consumer
class JsonVerifier(
private val json: String,
private val mode: JSONCompareMode
) : Consumer<EntityExchangeResult<ByteArray>> {
override fun accept(result: EntityExchangeResult<ByteArray>) {
try {
JSONAssert.assertEquals(json, getBodyAsString(result), mode)
} catch (ex: Exception) {
throw AssertionError("JSON parsing error", ex)
private fun getBodyAsString(result: EntityExchangeResult<ByteArray>): String {
val body = result.responseBody
if (body == null || body.isEmpty()) {
return ""
val mediaType = result.responseHeaders.contentType
val charset = mediaType?.charset ?: UTF_8
return String(body, charset)
fun WebTestClient.BodyContentSpec.jsonInStrict(json: String) = consumeWith(JsonVerifier(json, JSONCompareMode.STRICT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.