Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin + junit 5 - assertAll usage

Tags:

kotlin

junit5

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) }
)
like image 871
user2105282 Avatar asked Dec 03 '22 20:12

user2105282


1 Answers

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) }
)
like image 63
Jayson Minard Avatar answered Dec 08 '22 03:12

Jayson Minard