Skip to content

Commit 1be3eec

Browse files
committed
Replace KotlinBodySpec with extensions function
Prior to this commit, the ResponseSpec::expectBody extension function returned a special KotlinBodySpec, due to https://youtrack.jetbrains.com/issue/KT-5464. Now that KT-5464 has been fixed in Kotlin 1.6, we have no need for KotlinBodySpec, so this commit replaces it with a extension function that returns the Java BodySpec. Closes gh-28144
1 parent 5119aaf commit 1be3eec

File tree

1 file changed

+6
-43
lines changed

1 file changed

+6
-43
lines changed

spring-test/src/main/kotlin/org/springframework/test/web/reactive/server/WebTestClientExtensions.kt

+6-43
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package org.springframework.test.web.reactive.server
1919
import kotlinx.coroutines.flow.Flow
2020
import org.reactivestreams.Publisher
2121
import org.springframework.core.ParameterizedTypeReference
22-
import org.springframework.test.util.AssertionErrors.assertEquals
2322
import org.springframework.test.web.reactive.server.WebTestClient.*
2423

2524
/**
@@ -59,53 +58,17 @@ inline fun <reified T : Any> RequestBodySpec.body(flow: Flow<T>): RequestHeaders
5958
body(flow, object : ParameterizedTypeReference<T>() {})
6059

6160
/**
62-
* Extension for [ResponseSpec.expectBody] providing an `expectBody<Foo>()` variant and
63-
* a workaround for [KT-5464](https://youtrack.jetbrains.com/issue/KT-5464) which
64-
* prevents to use `WebTestClient.BodySpec` in Kotlin.
61+
* Extension for [ResponseSpec.expectBody] providing an `expectBody<Foo>()` variant
62+
* leveraging Kotlin reified type parameters. This extension is not subject ot type
63+
* erasure and retains actual generic type arguments.
6564
*
6665
* @author Sebastien Deleuze
66+
* @author Arjen Poutsma
6767
* @since 5.0
6868
*/
6969
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
70-
inline fun <reified B : Any> ResponseSpec.expectBody(): KotlinBodySpec<B> =
71-
expectBody(object : ParameterizedTypeReference<B>() {}).returnResult().let {
72-
object : KotlinBodySpec<B> {
73-
74-
override fun isEqualTo(expected: B): KotlinBodySpec<B> = it
75-
.assertWithDiagnostics { assertEquals("Response body", expected, it.responseBody) }
76-
.let { this }
77-
78-
override fun consumeWith(consumer: (EntityExchangeResult<B>) -> Unit): KotlinBodySpec<B> =
79-
it.assertWithDiagnostics { consumer.invoke(it) }.let { this }
80-
81-
override fun returnResult(): EntityExchangeResult<B> = it
82-
}
83-
}
84-
85-
/**
86-
* Kotlin compliant `WebTestClient.BodySpec` for expectations on the response body decoded
87-
* to a single Object, see [KT-5464](https://youtrack.jetbrains.com/issue/KT-5464) for
88-
* more details.
89-
* @since 5.0.6
90-
*/
91-
interface KotlinBodySpec<B> {
92-
93-
/**
94-
* Assert the extracted body is equal to the given value.
95-
*/
96-
fun isEqualTo(expected: B): KotlinBodySpec<B>
97-
98-
/**
99-
* Assert the exchange result with the given consumer.
100-
*/
101-
fun consumeWith(consumer: (EntityExchangeResult<B>) -> Unit): KotlinBodySpec<B>
102-
103-
/**
104-
* Exit the chained API and return an `ExchangeResult` with the
105-
* decoded response content.
106-
*/
107-
fun returnResult(): EntityExchangeResult<B>
108-
}
70+
inline fun <reified B : Any> ResponseSpec.expectBody(): BodySpec<B, *> =
71+
expectBody(object : ParameterizedTypeReference<B>() {})
10972

11073
/**
11174
* Extension for [ResponseSpec.expectBodyList] providing a `expectBodyList<Foo>()` variant.

0 commit comments

Comments
 (0)