diff --git a/munit/shared/src/main/scala/munit/Assertions.scala b/munit/shared/src/main/scala/munit/Assertions.scala index ec3e058b..2ac37118 100644 --- a/munit/shared/src/main/scala/munit/Assertions.scala +++ b/munit/shared/src/main/scala/munit/Assertions.scala @@ -189,15 +189,18 @@ trait Assertions extends MacroCompat.CompileErrorMacro { expectedExceptionMessage: Option[String], body: => Any )(implicit T: ClassTag[T], loc: Location): T = { + val expectedExceptionMsg = + s"expected exception of type '${T.runtimeClass.getName()}' but body evaluated successfully" try { body - fail( - s"expected exception of type '${T.runtimeClass.getName()}' but body evaluated successfully" - ) + fail(expectedExceptionMsg) } catch { case e: FailExceptionLike[_] if !T.runtimeClass.isAssignableFrom(e.getClass()) => throw e + case e: FailExceptionLike[_] + if e.getMessage.contains(expectedExceptionMsg) => + throw e case NonFatal(e) => if (T.runtimeClass.isAssignableFrom(e.getClass())) { if ( diff --git a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala index 0fcfcfae..1c4ce12c 100644 --- a/tests/shared/src/test/scala/munit/FailExceptionSuite.scala +++ b/tests/shared/src/test/scala/munit/FailExceptionSuite.scala @@ -7,4 +7,22 @@ class FailExceptionSuite extends BaseSuite { } assert(clue(e).isInstanceOf[Serializable]) } + + test("assertion-error-no-exception") { + try { + intercept[AssertionError] { + println("throwing no exception!") + } + throw new Exception("should not reach here") + } catch { + case e: FailException => + assert( + e.getMessage.contains( + "expected exception of type 'java.lang.AssertionError' but body evaluated successfully" + ) + ) + case _: Throwable => + fail("No FailException was thrown") + } + } }