[英]How to assert FlashAttributes in Spring WebTestClient?
[英]How to use Spring's WebTestClient to check for a string in Kotlin?
我正在尝试使用 WebTestClient 检查返回字符串的 Controller。 但由于某种原因,我得到了一个错误。
我使用 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。
WebFlux WebTestClient 和 Kotlin 已经存在类型干扰问题,但我使用特定类型。 字符串似乎在这里不起作用。 我错过了什么?
看起来您没有使用被确定为解决方法的扩展 function。 要使用它,请尝试更新测试的最后两行,如下所示:
webClient.get()
.uri("/hello/Foo")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isOk()
.expectBody<String>()
.isEqualTo("Hello Foo!")
这似乎工作正常。
以供参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.