Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ repository on GitHub.
* `junit-platform-console-standalone` is now part of the `junit-bom`
* `@org.junit.platform.suite.api.Disabled` can be used annotate a `@Suite`-annotated
class. If so annotated, the suite will not be executed.
* The `org.junit.jupiter.api.assertThrows` Kotlin API and its related functions have
added variants that use an exception class as a value rather than a type parameter.
This is to primarily to address the current issue/s when trying to use
`@ParameterizedTest` that produces exceptions as values.

[[v6.2.0-M1-junit-jupiter]]
=== JUnit Jupiter
Expand Down
191 changes: 191 additions & 0 deletions junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.AT_MOST_ONCE
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass

/**
* @see Assertions.fail
Expand Down Expand Up @@ -285,6 +286,39 @@ inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T {
}
}

/**
* Example usage:
* ```kotlin
* val exception = assertThrows(IllegalArgumentException::class) {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrows is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrows
*/
inline fun <T : Throwable> assertThrows(
expectedType: KClass<T>,
executable: () -> Unit
): T {
// no contract for `executable` because it is expected to throw an exception instead
// of being executed completely (see https://youtrack.jetbrains.com/issue/KT-27748)
val throwable: Throwable? =
try {
executable()
} catch (caught: Throwable) {
caught
} as? Throwable

return Assertions.assertThrows(expectedType.java) {
if (throwable != null) {
throw throwable
}
}
}

/**
* Example usage:
* ```kotlin
Expand All @@ -300,6 +334,25 @@ inline fun <reified T : Throwable> assertThrows(
executable: () -> Unit
): T = assertThrows({ message }, executable)

/**
* Example usage:
* ```kotlin
* val exception = assertThrows(IllegalArgumentException::class, "Should throw an Exception") {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrows is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrows
*/
inline fun <T : Throwable> assertThrows(
expectedType: KClass<T>,
message: String,
executable: () -> Unit
): T = assertThrows(expectedType, { message }, executable)

/**
* Example usage:
* ```kotlin
Expand Down Expand Up @@ -339,6 +392,49 @@ inline fun <reified T : Throwable> assertThrows(
)
}

/**
* Example usage:
* ```kotlin
* val exception = assertThrows(IllegalArgumentException::class, { "Should throw an Exception" }) {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrows is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrows
*/
@OptIn(ExperimentalContracts::class)
inline fun <T : Throwable> assertThrows(
expectedType: KClass<T>,
noinline message: () -> String,
executable: () -> Unit
): T {
contract {
callsInPlace(message, AT_MOST_ONCE)
// no contract for `executable` because it is expected to throw an exception instead
// of being executed completely (see https://youtrack.jetbrains.com/issue/KT-27748)
}

val throwable: Throwable? =
try {
executable()
} catch (caught: Throwable) {
caught
} as? Throwable

return Assertions.assertThrows(
expectedType.java,
{
if (throwable != null) {
throw throwable
}
},
message
)
}

/**
* Example usage:
* ```kotlin
Expand Down Expand Up @@ -366,6 +462,39 @@ inline fun <reified T : Throwable> assertThrowsExactly(executable: () -> Unit):
}
}

/**
* Example usage:
* ```kotlin
* val exception = assertThrows(IllegalArgumentException::class) {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrowsExactly is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrowsExactly
*/
inline fun <T : Throwable> assertThrowsExactly(
expectedType: KClass<T>,
executable: () -> Unit
): T {
// no contract for `executable` because it is expected to throw an exception instead
// of being executed completely (see https://youtrack.jetbrains.com/issue/KT-27748)
val throwable: Throwable? =
try {
executable()
} catch (caught: Throwable) {
caught
} as? Throwable

return Assertions.assertThrowsExactly(expectedType.java) {
if (throwable != null) {
throw throwable
}
}
}

/**
* Example usage:
* ```kotlin
Expand All @@ -381,6 +510,25 @@ inline fun <reified T : Throwable> assertThrowsExactly(
executable: () -> Unit
): T = assertThrowsExactly({ message }, executable)

/**
* Example usage:
* ```kotlin
* val exception = assertThrowsExactly(IllegalArgumentException::class, "Should throw an Exception") {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrowsExactly is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrowsExactly
*/
inline fun <T : Throwable> assertThrowsExactly(
expectedType: KClass<T>,
message: String,
executable: () -> Unit
): T = assertThrowsExactly(expectedType, { message }, executable)

/**
* Example usage:
* ```kotlin
Expand Down Expand Up @@ -420,6 +568,49 @@ inline fun <reified T : Throwable> assertThrowsExactly(
)
}

/**
* Example usage:
* ```kotlin
* val exception = assertThrowsExactly(IllegalArgumentException::class, { "Should throw an Exception" }) {
* throw IllegalArgumentException("Talk to a duck")
* }
* assertEquals("Talk to a duck", exception.message)
* ```
*
* This version of assertThrowsExactly is intended to be used in situations where reified Throwable's cannot be used,
* i.e. when using a @ParameterizedTest that provides exception classes as values.
* @see Assertions.assertThrowsExactly
*/
@OptIn(ExperimentalContracts::class)
inline fun <T : Throwable> assertThrowsExactly(
expectedType: KClass<T>,
noinline message: () -> String,
executable: () -> Unit
): T {
contract {
callsInPlace(message, AT_MOST_ONCE)
// no contract for `executable` because it is expected to throw an exception instead
// of being executed completely (see https://youtrack.jetbrains.com/issue/KT-27748)
}

val throwable: Throwable? =
try {
executable()
} catch (caught: Throwable) {
caught
} as? Throwable

return Assertions.assertThrowsExactly(
expectedType.java,
{
if (throwable != null) {
throw throwable
}
},
message
)
}

/**
* Example usage:
* ```kotlin
Expand Down
Loading
Loading