【发布时间】:2020-09-11 01:21:00
【问题描述】:
我正在尝试使用 WebTestClient 检查返回字符串的控制器。但由于某种原因,我得到了一个错误。
我使用 Kotlin,所以我尝试将我找到的 Java 示例应用到它,但我不知道如何正确地做到这一点。我错过了什么?
@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!")
}
}
当我尝试使用 String 或 java.lang.String 而不是 Nothing 时,我收到错误消息:
类型参数不在其范围内。 预期:没有! 找到:字符串!
当我使用 Nothing 时,我得到了 NPE。
已经有Type interference issue with the WebFlux WebTestClient and Kotlin,但我使用的是特定类型。字符串似乎在这里不起作用。我错过了什么?
【问题讨论】:
标签: spring spring-boot unit-testing kotlin webtestclient