I am trying to use the WebTestClient to check a Controller that returns a string. But for some reason I get an error.
I use Kotlin so I tried to apply the Java examples I have found to it but I can't figure out how to do it right. What am I missing?
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HelloResourceIT {
@Test
fun shouldReturnGreeting(@Autowired webClient: WebTestClient) {
webClient.get()
.uri("/hello/Foo")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isOk()
.expectBody(String::class.java)
.isEqualTo<Nothing>("Hello Foo!")
}
}
When I try to use String
or java.lang.String
instead of Nothing
I get the error message:
Type argument is not within its bounds. Expected: Nothing! Found:String!
When I use Nothing
I get a NPE.
There is already Type interference issue with the WebFlux WebTestClient and Kotlin but I works with a specific type. String does not seem to work here. What I am missing?
It looks like you are not using the extension function that was identified as a work-around. To use it, try updating the last two lines of your test as follows:
webClient.get()
.uri("/hello/Foo")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isOk()
.expectBody<String>()
.isEqualTo("Hello Foo!")
which appears to work correctly.
For reference: