2

I'm using Spring 5 WebClient test features and would like to extract body matched by the jsonPath expression but can't find any suitable way of doing it. For example (the code is in Kotlin but I hope this doesn't make any difference):

client!!.get().uri("/app/metrics")
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .jsonPath("$.names")
                .isArray

The actual json body is:

{
"names": [
"jvm.buffer.memory.used",
"jvm.memory.used",
"jvm.buffer.count",
"jvm.gc.memory.allocated",
"logback.events",
"process.uptime",
"jvm.memory.committed",
"system.load.average.1m",
"jvm.gc.max.data.size",
"jvm.buffer.total.capacity",
"jvm.memory.max",
"system.cpu.count",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.threads.live",
"process.start.time",
"jvm.threads.peak",
"jvm.gc.live.data.size",
"jvm.gc.memory.promoted",
"process.cpu.usage"
]
}

So I'd like to get list of names into list variable and use it. Is there any way to do it with current jsonPath support?

yyunikov
  • 5,719
  • 2
  • 43
  • 78

1 Answers1

0

Something like this should work (tried with springBootVersion = '2.1.0.RELEASE'):

client.get().uri("/app/metrics")
    .accept(MediaType.APPLICATION_JSON_UTF8)
    .exchange()
    .expectStatus().isOk
    .expectBody()
    .jsonPath("$.names")
    .value<JSONArray> { json ->
        json.toList().forEach { println(it) }
    }

will print:

jvm.buffer.memory.used
jvm.memory.used
jvm.buffer.count
jvm.gc.memory.allocated
logback.events
process.uptime
jvm.memory.committed
system.load.average.1m
jvm.gc.max.data.size
jvm.buffer.total.capacity
jvm.memory.max
system.cpu.count
jvm.threads.daemon
system.cpu.usage
jvm.threads.live
process.start.time
jvm.threads.peak
jvm.gc.live.data.size
jvm.gc.memory.promoted
process.cpu.usage
maslick
  • 2,903
  • 3
  • 28
  • 50