3

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?

Apollo
  • 1,296
  • 2
  • 11
  • 24

1 Answers1

9

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:

Thomas Portwood
  • 1,031
  • 8
  • 13
  • 1
    This works. I messed up the import thus my IDE was not providing me any information about it. Thank you! – Apollo May 25 '20 at 20:32