关于kotlin:AssertJ:类型推断失败:没有足够的信息来推断org.assertj.core.api.Assertions.fail中的参数T

AssertJ: Type inference failed: Not enough information to infer parameter T in org.assertj.core.api.Assertions.fail

在一个纯Kotlin项目中,我正在使用JUnit Jupiter 5.5.2和AssertJ 3.10.0。以下测试成功执行:

1
2
3
4
5
6
7
8
@Test
fun `Validates something`() = runBlocking {
    try {
        // Assert something
    } catch (t: Throwable) {
        fail("Should not throw $t")
    }
}

一旦我更新到AssertJ 3.11.1,测试构建就会失败,并显示以下消息:

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

如果我使用fail<Nothing>("Should not throw $t"),则使用No test were found

我试图弄清楚发生了什么-尽管没有成功。

有关的

  • JUnit5问题#1209:如果未明确指定通用类型,则无法在Kotlin中调用"失败"


似乎问题在于runBlocking表达式主体。如果将其变成块体,则无论您在fail方法上指定使用哪种类型(以Nothing为例,可以是AnyAny?或任何其他类型),它都将起作用):

1
2
3
4
5
6
7
8
9
10
@Test
fun `Validates something`() {
    runBlocking {
        try {
            // Assert something
        } catch (t: Throwable) {
            fail<Nothing>("Should not throw $t")
        }
    }
}