Skip to content

Introduce Kotlin Assertion Helper Library #924

Closed
@JLLeitschuh

Description

@JLLeitschuh

Feature Request

This is mostly a "nice to have" syntactically but the current a select few methods that all take [SomeWrapperType]<Executable> don't play as nicely as they could in kotlin.

Someone could easily create a kotlin DSL library wrapper over junit 5.
I did this with the Google Guice DI library creating Kotlin Guiced. I could just as easily do the same thing for Junit5 but if there is an interest it would be nice if the small DSL library was vendored by the Junit team.

The methods that I've currently found that don't play the nicest are the following:

assertAll(Stream<Executable> executables):

In kotlin calling this method becomes:

assertAll(someCollection.stream().map {
    Executable {
         assertEquals(6,6)
    }
})

This could be simplified by providing a method:

fun assertAll(executables: Stream<() -> Unit>) = assertAll(executables.map { Executable(it) })

This would allow for the much nicer syntax:

assertAll(someCollection.stream().map {
    { assertEquals(6,6) }
})

// Or even this:
assertAll(someCollection.stream().map {
    listOf(
        { assertEquals(6,6) }
        { assertEquals(3,3) }
    )
}.flatMap { it.stream() })

assertAll(Executable... executables):

In kotlin calling this method becomes:

assertAll(
    Executable { assertEquals(6, 6) },
    Executable { assertEquals(3, 3) },
    Executable { assertEquals(10_000, 10_000) }
)

This could be simplified by providing a method:

// This calls the above suggested method.
fun assertAll(vararg executables: () -> Unit) = assertAll(executables.toList().stream())

This would allow for the much nicer syntax:

assertAll(
   { assertEquals(6, 6) },
   { assertEquals(3, 3) },
   { assertEquals(10_000, 10_000) }
)

Similar methods would need to be provided for the methods that take String header as well.

assertThrows(Class<T> expectedType, Executable executable)

Calling in Kotlin it becomes:

assertThrows(IllegalArgumentException::class.java) { throw IllegalArgumentException() }

Yeay! We get auto conversion because this method takes Executable executable not a collection. However the IllegalArgumentException::class.java is kind of clunky.

This one could use reified types to make this nicer!

If we provide a method:

inline fun <reified T> assertThrows(executable : () -> Unit) = assertThrows(T::class.java, Executable(executable))

Then the syntax to call it in kotlin becomes:

assertThrows<IllegalArgumentException> { throw IllegalArgumentException() }

Note
These problems don't exist with all methods that take Executable.
For example, this is completely valid given the current Junit5 API:

assertTimeout(Duration.ZERO) { }

Deliverables

  • Create a junit-jupiter-kotlin-api.
  • Provide Kotlin friendly DSL in the junit-jupiter-kotlin-api library.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions