user2105282
user2105282

Reputation: 804

kotlin + junit 5 - assertAll usage

I use next version of 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 version is

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

I try to use new assertion functionality from junit 5 with kotlin like

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

but code analyzer says that no suitable version of the method is found.

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

if I write the code like this it works fine. What is the trick?

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

Upvotes: 7

Views: 8152

Answers (1)

Jayson Minard
Jayson Minard

Reputation: 85936

Use the Kotlin function assertAll() and not the static function Assertions.assertAll() and you will be happy. Change your import from:

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

to:

import org.junit.jupiter.api.assertAll

And now this code will work:

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

You would be interested to know that JUnit 5 includes Kotlin helpers within its main source code, directly from the JUnit team!

If you really, really, really want to use the straight Java version, you need to type your Lambda:

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

Upvotes: 21

Related Questions