summaryrefslogtreecommitdiff
path: root/test/junit
diff options
context:
space:
mode:
authorDen Shabalin <den.shabalin@gmail.com>2013-08-09 15:36:26 +0200
committerDen Shabalin <den.shabalin@gmail.com>2013-08-14 11:45:47 +0200
commit834e29ff7f2047054e4af4c05311943bcbe480a7 (patch)
treeb1bdc4a8b849f06b40aac7ec7a3aca57df499cec /test/junit
parentf17fb5eaa545490c761acd4f6979a619f919ac86 (diff)
downloadscala-834e29ff7f2047054e4af4c05311943bcbe480a7.tar.gz
scala-834e29ff7f2047054e4af4c05311943bcbe480a7.tar.bz2
scala-834e29ff7f2047054e4af4c05311943bcbe480a7.zip
add assertThrows testing utility function
This is much easier to use than built-in JUnit method-level checks.
Diffstat (limited to 'test/junit')
-rw-r--r--test/junit/scala/tools/testing/AssertThrowsTest.scala25
-rw-r--r--test/junit/scala/tools/testing/AssertUtil.scala19
2 files changed, 44 insertions, 0 deletions
diff --git a/test/junit/scala/tools/testing/AssertThrowsTest.scala b/test/junit/scala/tools/testing/AssertThrowsTest.scala
new file mode 100644
index 0000000000..ecc46244bd
--- /dev/null
+++ b/test/junit/scala/tools/testing/AssertThrowsTest.scala
@@ -0,0 +1,25 @@
+package scala.tools
+package testing
+
+import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import AssertUtil.assertThrows
+
+@RunWith(classOf[JUnit4])
+class AssertThrowsTest {
+ class Foo extends Exception
+ class Bar extends Exception
+
+ @Test
+ def catchFoo = assertThrows[Foo] { throw new Foo }
+
+ @Test
+ def rethrowBar =
+ try assertThrows[Foo] { throw new Bar }
+ catch {
+ case bar: Bar =>
+ case e: Throwable => fail(s"expected Bar but got $e")
+ }
+} \ No newline at end of file
diff --git a/test/junit/scala/tools/testing/AssertUtil.scala b/test/junit/scala/tools/testing/AssertUtil.scala
new file mode 100644
index 0000000000..9efa9327a7
--- /dev/null
+++ b/test/junit/scala/tools/testing/AssertUtil.scala
@@ -0,0 +1,19 @@
+package scala.tools
+package testing
+
+/** This module contains additional higher-level assert statements
+ * that are ultimately based on junit.Assert primitives.
+ */
+object AssertUtil {
+ /** Check if exception T was thrawn during evaluation of f. If any
+ * other exception or throwable is found instead it will be re-thrown.
+ */
+ def assertThrows[T <: Exception](f: => Any)(implicit manifest: Manifest[T]): Unit =
+ try f
+ catch {
+ case e: Exception =>
+ val clazz = manifest.erasure.asInstanceOf[Class[T]]
+ if (!clazz.isAssignableFrom(e.getClass))
+ throw e
+ }
+} \ No newline at end of file