【问题标题】:kotlin + junit 5 - assertAll usagekotlin + junit 5 - assertAll 用法
【发布时间】:2018-05-23 14:46:50
【问题描述】:

我使用 junit 5 的下一个版本

    <junit.jupiter.version>5.2.0</junit.jupiter.version>
    <junit.platform.version>1.2.0</junit.platform.version>
    <junit.vintage.version>5.2.0</junit.vintage.version>

Kotlin 版本是

    <kotlin.version>1.2.31</kotlin.version>

我尝试将 junit 5 中的新断言功能与 kotlin 类似

assertAll("person",
    { assertEquals("John", person.firstName) },
    { assertEquals("Doe", person.lastName) }
)

但是代码分析器说没有找到合适的方法版本。

Error:(28, 9) Kotlin: None of the following functions can be called with the arguments supplied: 
    public fun assertAll(vararg executables: () -> Unit): Unit defined in org.junit.jupiter.api
    public fun assertAll(heading: String?, vararg executables: () -> Unit): Unit defined in org.junit.jupiter.api

如果我这样编写代码,它可以正常工作。有什么诀窍?

assertAll("person",
    Executable { assertEquals("John", person.firstName) },
    Executable { assertEquals("Doe", person.lastName) }
)

【问题讨论】:

    标签: kotlin junit5


    【解决方案1】:

    使用 Kotlin 函数 assertAll() 而不是静态函数 Assertions.assertAll(),你会很开心。更改您的导入来源:

    import org.junit.jupiter.api.Assertions.assertAll
    

    到:

    import org.junit.jupiter.api.assertAll
    

    现在这段代码可以工作了:

    assertAll("person",
        { assertEquals("John", person.firstName) },
        { assertEquals("Doe", person.lastName) }
    )
    

    您会很想知道 JUnit 5 在其主要源代码中包含 Kotlin 助手,直接来自 JUnit 团队!

    如果你真的、真的、真的想使用直接的 Java 版本,你需要输入你的 Lambda:

    assertAll("person",
        Executable { assertEquals("John", person.firstName) },
        Executable { assertEquals("Doe", person.lastName) }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 2018-02-21
      • 2018-06-06
      相关资源
      最近更新 更多