Skip to content

Commit 4eab5e0

Browse files
committed
Show expected in AssertUtil.assertThrows
Error should mention what was expected exception: wrong exception: expected Foo but was Bar Current test error is simply re-throwing the failed exception: failed: Bar
1 parent 05d028e commit 4eab5e0

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/testkit/scala/tools/testkit/AssertUtil.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,7 @@ object AssertUtil {
8787
*/
8888
def assertThrows[T <: Throwable: ClassTag](body: => Any,
8989
checkMessage: String => Boolean = s => true): Unit = {
90-
try {
91-
body
92-
fail("Expression did not throw!")
93-
} catch {
94-
case e: T if checkMessage(e.getMessage) =>
95-
}
90+
assertThrown[T](t => checkMessage(t.getMessage))(body)
9691
}
9792

9893
def assertThrown[T <: Throwable: ClassTag](checker: T => Boolean)(body: => Any): Unit =
@@ -106,7 +101,7 @@ object AssertUtil {
106101
ae.addSuppressed(failed)
107102
throw ae
108103
case NonFatal(other) =>
109-
val ae = new AssertionError(s"Exception not a ${implicitly[ClassTag[T]]}: $other")
104+
val ae = new AssertionError(s"Wrong exception: expected ${implicitly[ClassTag[T]]} but was ${other.getClass.getName}")
110105
ae.addSuppressed(other)
111106
throw ae
112107
}

test/junit/scala/tools/testkit/AssertThrowsTest.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ class AssertThrowsTest {
3131
def catchSubclass = assertThrows[Foo] { throw new SubFoo }
3232

3333
@Test
34-
def rethrowBar =
35-
assertTrue("exception wasn't rethrown", {
34+
def wrongThrow =
35+
assertTrue("Wrong exception thrown", {
3636
try {
3737
assertThrows[Foo] { throw new Bar }
3838
false
3939
} catch {
40-
case bar: Bar => true
41-
case e: Throwable => fail(s"expected Bar but got $e"); false
40+
case b: Bar => fail("Bar shouldn't have been rethrown"); false
41+
case e: AssertionError => true
42+
case t: Throwable => fail(s"expected AssertionError but got $t"); false
4243
}
4344
})
4445

@@ -71,7 +72,7 @@ class AssertThrowsTest {
7172
} catch {
7273
case ae: AssertionError =>
7374
assertEquals(1, ae.getSuppressed.length)
74-
assertEquals("Exception not a scala.tools.testkit.AssertThrowsTest$Foo: scala.tools.testkit.AssertThrowsTest$Bar", ae.getMessage)
75+
assertEquals("Wrong exception: expected scala.tools.testkit.AssertThrowsTest$Foo but was scala.tools.testkit.AssertThrowsTest$Bar", ae.getMessage)
7576
assertEquals(classOf[Bar], ae.getSuppressed.head.getClass)
7677
case t: Throwable => fail("Expected an AssertionError: $t")
7778
}

test/scalacheck/scala/reflect/quasiquotes/QuasiquoteProperties.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ trait Helpers {
6868
} catch {
6969
case u: Throwable =>
7070
if (!clazz.isAssignableFrom(u.getClass))
71-
assert(false, s"wrong exception: $u")
71+
assert(false, s"wrong exception: expected ${clazz.getName} but was ${u.getClass.getName}")
7272
true
7373
}
7474
if(!thrown)

0 commit comments

Comments
 (0)