【问题标题】:AssertJ: Type inference failed: Not enough information to infer parameter T in org.assertj.core.api.Assertions.failAssertJ:类型推断失败:没有足够的信息来推断 org.assertj.core.api.Assertions.fail 中的参数 T
【发布时间】:2020-03-11 12:26:00
【问题描述】:

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

@Test
fun `Validates something`() = runBlocking {
    try {
        // Assert something
    } catch (t: Throwable) {
        fail("Should not throw $t")
    }
}

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

类型推断失败:没有足够的信息来推断有趣的参数 T 失败(p0:字符串!):T!
请明确指定。

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

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

相关

【问题讨论】:

  • 不会使用fail<Any> 甚至fail<Any?> 帮助吗?

标签: kotlin type-inference junit5 assertj


【解决方案1】:

看来问题出在runBlocking 表达式主体上。如果你把它变成一个块体,那么无论你在fail方法上指定哪种类型,它都可以工作(以Nothing为例,可以是AnyAny?或任何其他类型):

@Test
fun `Validates something`() {
    runBlocking {
        try {
            // Assert something
        } catch (t: Throwable) {
            fail<Nothing>("Should not throw $t")
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多