Skip to content

Commit

Permalink
Inline lambdas passed to assertThrows to support suspending functions
Browse files Browse the repository at this point in the history
Resolves #1851.
  • Loading branch information
ashdavies authored and marcphilipp committed Mar 19, 2020
1 parent dd98521 commit c82acf3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object Versions {
val bartholdy = "0.2.3"
val classgraph = "4.8.59"
val commonsIo = "2.6"
val coroutines = "1.3.3"
val groovy = "3.0.0-rc-2"
val log4j = "2.12.1"
val mockito = "3.2.4"
Expand Down
1 change: 1 addition & 0 deletions dependencies/dependencies.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ dependencies {
api("com.tngtech.archunit:archunit-junit5-api:${Versions.archunit}")
api("com.tngtech.archunit:archunit-junit5-engine:${Versions.archunit}")
api("org.slf4j:slf4j-jdk14:${Versions.slf4j}")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ fun assertAll(heading: String?, vararg executables: () -> Unit) =
* ```
* @see Assertions.assertThrows
*/
inline fun <reified T : Throwable> assertThrows(noinline executable: () -> Unit): T =
Assertions.assertThrows(T::class.java, Executable(executable))
inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T {
val result = runCatching(executable)

return Assertions.assertThrows(T::class.java) { result.getOrThrow() }
}

/**
* Example usage:
Expand All @@ -108,7 +111,7 @@ inline fun <reified T : Throwable> assertThrows(noinline executable: () -> Unit)
* ```
* @see Assertions.assertThrows
*/
inline fun <reified T : Throwable> assertThrows(message: String, noinline executable: () -> Unit): T =
inline fun <reified T : Throwable> assertThrows(message: String, executable: () -> Unit): T =
assertThrows({ message }, executable)

/**
Expand All @@ -121,8 +124,11 @@ inline fun <reified T : Throwable> assertThrows(message: String, noinline execut
* ```
* @see Assertions.assertThrows
*/
inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, noinline executable: () -> Unit): T =
Assertions.assertThrows(T::class.java, Executable(executable), Supplier(message))
inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, executable: () -> Unit): T {
val result = runCatching(executable)

return Assertions.assertThrows(T::class.java, Executable { result.getOrThrow() }, Supplier(message))
}

/**
* Example usage:
Expand Down
1 change: 1 addition & 0 deletions junit-jupiter-engine/junit-jupiter-engine.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ dependencies {
testImplementation(project(":junit-platform-runner"))
testImplementation(project(":junit-platform-testkit"))
testImplementation("org.jetbrains.kotlin:kotlin-stdlib")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
testImplementation("org.codehaus.groovy:groovy-all")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package org.junit.jupiter.api

import java.util.stream.Stream
import kotlin.reflect.KClass
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals
import org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith
import org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError
Expand Down Expand Up @@ -60,6 +61,13 @@ class KotlinAssertionsTests {
assertThrows<AssertionError>({ "should fail" }) { fail(null as Throwable?) }
}

@Test
fun `expected context exception testing`() = runBlocking<Unit> {
assertThrows<AssertionError>("Should fail async") {
suspend { fail("Should fail async") }()
}
}

@TestFactory
fun assertDoesNotThrow(): Stream<out DynamicNode> = Stream.of(
dynamicContainer("succeeds when no exception thrown", Stream.of(
Expand Down

0 comments on commit c82acf3

Please sign in to comment.