Since WebTestClientExtensions.expectBody uses plain Class instead of ParameterizedTypeReference, it cannot correctly deserialize generic types.
package net.example
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
class ExampleApplication
@RestController
class ExampleController {
@GetMapping("example")
fun index(): ExampleWrapper<ExampleData> = ExampleWrapper(ExampleData("EXAMPLE"))
}
data class ExampleWrapper<T>(val payload: T)
data class ExampleData(val string: String)
package net.example
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
@WebFluxTest(ExampleController::class)
class ExampleControllerTest @Autowired constructor(
private val webClient: WebTestClient
) {
@Test
fun test() {
webClient.get()
.uri("/example")
.exchange()
.expectBody<ExampleWrapper<ExampleData>>()
.consumeWith {
Assertions.assertEquals(ExampleData::class, it.responseBody!!.payload::class)
}
}
}
java.lang.AssertionError:
Expected :class net.example.ExampleData
Actual :class java.util.LinkedHashMap