From 96ed87d81bbbadd6295c232c21a5cc897237078a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 26 Mar 2020 00:19:53 +0300 Subject: [PATCH] assertFailsWith: set unexpected exception as a cause of assertion error #KT-23514 --- .../kotlin/kotlin/test/tests/BasicAssertionsTest.kt | 10 +++++++--- .../js/src/main/kotlin/kotlin/test/JsImpl.kt | 2 +- .../kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt index 7e33a253f42..6d541f3c8c8 100644 --- a/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt +++ b/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/BasicAssertionsTest.kt @@ -35,9 +35,11 @@ class BasicAssertionsTest { assertTrue(true) // at least one assertion required for qunit withDefaultAsserter run@{ + val rootCause = IllegalArgumentException() try { - assertFailsWith { throw IllegalArgumentException() } + assertFailsWith { throw rootCause } } catch (e: AssertionError) { + if (e.cause !== rootCause) throw AssertionError("Expected to fail with correct cause") return@run } throw AssertionError("Expected to fail") @@ -62,9 +64,11 @@ class BasicAssertionsTest { @Test fun testAssertFailsWithClassFails() { - checkFailedAssertion { - assertFailsWith(IllegalArgumentException::class) { throw IllegalStateException() } + val rootCause = IllegalStateException() + val actual = checkFailedAssertion { + assertFailsWith(IllegalArgumentException::class) { throw rootCause } } + assertSame(rootCause, actual.cause, "Expected to fail with correct cause") checkFailedAssertion { assertFailsWith(Exception::class) { } diff --git a/libraries/kotlin.test/js/src/main/kotlin/kotlin/test/JsImpl.kt b/libraries/kotlin.test/js/src/main/kotlin/kotlin/test/JsImpl.kt index 5223cf7400f..e8c9ff3d0fb 100644 --- a/libraries/kotlin.test/js/src/main/kotlin/kotlin/test/JsImpl.kt +++ b/libraries/kotlin.test/js/src/main/kotlin/kotlin/test/JsImpl.kt @@ -34,7 +34,7 @@ internal actual fun checkResultIsFailure(exceptionClass: KClass< @Suppress("UNCHECKED_CAST") return e as T } - asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e") + asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e", e) } ) } diff --git a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt index 93e690fff0e..8efe94454ab 100644 --- a/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt +++ b/libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt @@ -25,7 +25,7 @@ internal actual fun checkResultIsFailure(exceptionClass: KClass< return e as T } - asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.java} to be thrown, but was $e") + asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.java} to be thrown, but was $e", e) } ) }