WebFlux WebTestClient和Kotlin的类型干扰问题 [英] Type interference issue with the WebFlux WebTestClient and Kotlin

查看:66
本文介绍了WebFlux WebTestClient和Kotlin的类型干扰问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Webflux和Kotlin为新应用程序构建原型. Spring Webflux包含一个用于单元测试的WebTestClient.根据文档,我应该能够测试REST调用的结果,如下所示:

I'm building a prototype for a new application using Spring Webflux and Kotlin. Spring Webflux contains a WebTestClient for unit tests. According to the documentation I should be able to test the results of a REST call like this:

@Test
fun getVersion_SingleResult_ContentTypeJson_StatusCodeOk_ContentEqualsVersion() {
    //given
    var version = Version("Test", "1.0")
    val handler = ApiHandler(version!!)
    val client = WebTestClient.bindToRouterFunction(ApiRoutes(handler).apiRouter()).build()

    //expect
    val response = client.get().uri("/api/version/").exchange()
    response.expectStatus().isOk
    response.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
    response.expectBody(Version::class.java).isEqualTo(version)
}

但是,我遇到了一些类型干扰问题.问题在于"expectBody"和"isEqualTo"的组合.

However, I'm running into some type interference issues. The problem is in the combination of 'expectBody' and 'isEqualTo'.

我得到的错误是:

科特琳:类型推断失败:没有足够的信息来有趣地推断参数T isEqualTo(p0:版本!):T!请明确指定.

Kotlin: Type inference failed: Not enough information to infer parameter T in fun isEqualTo(p0: Version!): T! Please specify it explicitly.

使用的方法具有以下签名:

The used methods have the following signatures:

<B> WebTestClient.BodySpec<B, ?> expectBody(Class<B> var1);

public interface BodySpec<B, S extends WebTestClient.BodySpec<B, S>> {
    <T extends S> T isEqualTo(B var1);
}

可悲的是,我遇到了有关泛型知识的限制以及Kotlin和Java之间的差异,这意味着我不确定应该如何指定.

Sadly I'm running into the limits of my knowledge of generics and the differences between Kotlin and Java which means that I'm not sure how I should specify this.

就像我在下面说的那样,当我使用isEqualTo<Nothing>(version)时它会编译.但是,当isEqualTo结束而没有失败时,这将导致NullPointerException.这似乎是因为'isEqualTo'方法返回一个值,该值现在被定义为'Nothing'类型.

Like I said below, it compiles when I use isEqualTo<Nothing>(version). However, this causes a NullPointerException when the isEqualTo ends without a failure. This seems to be because the 'isEqualTo' method returns a value, which is now defined as the 'Nothing' type.

推荐答案

此已知问题来自Kotlin类型推断限制( KT-5464 )已在Spring JIRA上报告为 SPR-自Spring Framework 5.0.6 +/Spring Boot 2.0.2+起,已修复15692 .确保通过编写.expectBody<Foo>()使用Kotlin扩展名.另请参阅我在Kotlin中创建的包含具体WebTestClient​​示例的存储库

This known issue coming from a Kotlin type inference limitation (KT-5464) already reported on Spring JIRA as SPR-15692 has been fixed as of Spring Framework 5.0.6+ / Spring Boot 2.0.2+. Make sure to use the Kotlin extension by writing .expectBody<Foo>(). See also the repository with concrete WebTestClient examples in Kotlin I have created

这篇关于WebFlux WebTestClient和Kotlin的类型干扰问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆