I am trying to model the expected exception behavior for common test frameworks (like JUnit or TestNG).
Here is what I could come up with so far (while working):
trait ExpectAsserts { self : { def fail (message : String) def success (message : String) } => def expect[T](exceptionClass : Class[T])(test : => Unit) { try { test fail("exception not thrown") } catch { case expected : T => success("got exception " + expected) case other : Exception => fail("expected "+ exceptionClass + " but " + other + " thrown instead.") } } } object Main extends ExpectAsserts { def main (args : Array[String]) { expect(classOf[ArithmeticException]) { throw new IllegalArgumentException
The fragment has a main method that executes the code. My problem is that the thrown exception is included in the first pattern matching statement:
case expected : T
Although I do say that the exception must be of type T , which would be an IllegalArgumentException .
Any ideas?
source share