Kotlin泛型问题,应该是Nothing的子类型

时间:2018-02-22 06:38:20

标签: kotlin spring-test kotlin-generics spring-kotlin

我正在Kotlin上编写一个简单的Spring测试,并且有一些我无法理解的泛型编译错误。功能如下:

@Test
fun actuatorRootReturnsOnlyAllowed() {
    val expectBody:WebTestClient.BodySpec<Map<String, Map<String, Any>>, *> = client!!.get().uri("/app")
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .exchange()
            .expectStatus().isOk
            .expectBody(object : ParameterizedTypeReference<Map<String, Map<String, Any>>>() {})

    expectBody.consumeWith<Map<String, Map<String, Any>>> { result->
        val actuatorLinks = result.responseBody!!["_links"]
        Assert.assertTrue(actuatorLinks!!.containsKey("self"))
        Assert.assertTrue(actuatorLinks.containsKey("health"))
        Assert.assertTrue(actuatorLinks.containsKey("info"))
        Assert.assertTrue(actuatorLinks.containsKey("loggers"))
        Assert.assertTrue(actuatorLinks.containsKey("loggers-name"))
    }
}

问题在于行expectBody.consumeWith。出于某种原因,Kotlin期望类型Nothing带有编译错误:Error:(53, 32) Kotlin: Type argument is not within its bounds: should be subtype of 'Nothing!'consumeWith方法的实际类型为EntityExchangeResult<T>,但方法仍需要Nothing。我会期待expectBody.consumeWithEntityExchangeResult<<Map<String, Map<String, Any>>>>expectBody.consumeWith<Map<String, Map<String, Any>>>之类的内容。

但是对于Nothing类型,我会收到NullPointerException

java.lang.NullPointerException
    at com.example.ActuatorEndpointsTest.actuatorRootReturnsOnlyAllowed(ActuatorEndpointsTest.kt:52)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

下面是Java中相同的工作等价物:

  @Test
  public void actuatorRootReturnsOnlyAllowed() {
    client.get().uri("/app")
        .accept(MediaType.APPLICATION_JSON_UTF8)
        .exchange()
        .expectStatus().isOk()
        .expectBody(new ParameterizedTypeReference<Map<String, Map<String, Object>>>(){})
        .consumeWith(result -> {
          final Map<String, Object> actuatorLinks = result.getResponseBody().get("_links");
          assertTrue(actuatorLinks.containsKey("self"));
          assertTrue(actuatorLinks.containsKey("health"));
          assertTrue(actuatorLinks.containsKey("info"));
          assertTrue(actuatorLinks.containsKey("loggers"));
          assertTrue(actuatorLinks.containsKey("loggers-name"));
        });
  }

有人能解释为什么Kotlin期望Nothing以及如何纠正此代码?

0 个答案:

没有答案