Kotlin 科特林+;JUnit5-assertAll使用
我使用JUnit5的下一个版本Kotlin 科特林+;JUnit5-assertAll使用,kotlin,junit5,Kotlin,Junit5,我使用JUnit5的下一个版本 <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> 但代码分析器说,并没有找到合适的方法版本 Err
<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>
但代码分析器说,并没有找到合适的方法版本
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) }
)
assertAll("person",
Executable { assertEquals("John", person.firstName) },
Executable { assertEquals("Doe", person.lastName) }
)
使用Kotlin函数
assertAll()
而不是静态函数Assertions.assertAll()
,您会很高兴的。将您的导入更改为:
import org.junit.jupiter.api.Assertions.assertAll
致:
现在,这个代码将起作用:
assertAll("person",
{ assertEquals("John", person.firstName) },
{ assertEquals("Doe", person.lastName) }
)
您可能有兴趣知道,JUnit5在其主要源代码中直接从JUnit团队包含Kotlin helpers
如果您真的、真的、真的想使用纯Java版本,则需要键入Lambda:
import org.junit.jupiter.api.assertAll
assertAll("person",
{ assertEquals("John", person.firstName) },
{ assertEquals("Doe", person.lastName) }
)
assertAll("person",
Executable { assertEquals("John", person.firstName) },
Executable { assertEquals("Doe", person.lastName) }
)