1

我正在 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!'. 方法的实际类型consumeWithEntityExchangeResult<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以及如何更正此代码?

4

0 回答 0