Please add something like the following method to KotlinBodySpec<B>
in org.springframework.test.web.reactive.server
:
fun <B> KotlinBodySpec<List<B>>.consumeListWith(consumer: (List<B>) -> Unit): KotlinBodySpec<List<B>> {
return consumeWith { result ->
consumer(result.responseBody!!)
}
}
So that instead of the usual:
expectBody<List<String>>().consumeWith { result ->
val list = result.responseBody!!
assertThat(list)....
We could then simply write:
expectBody<List<String>>().consumeListWith { list ->
assertThat(list)....
At least I wouldn't know what else to do with an EntityExchangeResult
anyway, except for well get the actual result from it.
Comment From: sbrannen
I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.
Comment From: poutsma
The KotlinBodySpec
was introduced as a workaround for KT-5464. Since that issue has been fixed, we will replace the workaround (and KotlinBodySpec
) in 6.0 with a normal extension function (see #28144), and expect Kotlin users to use the Java versions of expectBody
and expectBodyList
.
Is there a specific reason you'd like to have a consumeListWith
instead of using expectBodyList
?
Comment From: lathspell
Is there a specific reason you'd like to have a consumeListWith instead of using expectBodyList ?
It's a bit cumbersome to use:
.expectBodyList<Person>().consumeWith<ListBodySpec<Person>> { result->
val list = result.responseBody!!
assertThat(....)
}
Comment From: sdeleuze
For this use case, it is probably shorter to do:
.expectBodyList<Person>().value<ListBodySpec<Person>> {
assertThat(it)...
}
Also notice that expectBodyList
is designed to provide nice shortcut like responseSpec.expectBodyList<Foo>().contains(...)
so check if that apply to your use case or not.
Unless I miss something, I would not recommend to add a specific Kotlin extension for this since our regular policy is to stay close to the Java APIs and mainly provide shortcuts for reified type parameters.
That said, there is a remaining painful point to solve here since in Java we can use .expectBodyList<Person>().value()
while in Kotlin we have to specify the generic type explicitly .expectBodyList<Person>().value<ListBodySpec<Person>> { }
. This should be fixed on Kotlin side not on Spring side.
I am not sure why KT-40804 did not fix it, maybe it relates to a variation linked to KT-5464 or KT-22208. I will ask to Kotlin team.
Comment From: lathspell
Thanks for the tip, value {}
is nice. Hopefully Kotlin fixes the generic type issues soon.
Comment From: sdeleuze
I am closing this issue, we will comment here if/when we get a feedback from the Kotlin team.
Comment From: sdeleuze
Kotlin team confirmed this need to be fixed on Kotlin side, see related comment in KT-5464.