Exception matcher in Specs BDD library for Scala
Im using the Specs BDD library for writing Scala unit tests (http://code.google.com/p/specs) .In my code if i want to assert that a throws an exception of type ClassNotFoundException, then i can write the following code:
a must throwA[开发者_开发百科ClassNotFoundException]
However,i want to test the reverse case,i.e.i want to assert that a "does not" throw an exception of type ClassNotFoundException.
I tried using not negation matcher, as follows:
a must throwA[ClassNotFoundException].not
But that didnt work. Im getting compilation errors. So, is there any way i can assert that an exception of type ClassNotFoundException for example, is not thrown ?
Please Help Thank You
Yes there is a parsing issue when compiling:
a must throwA[ClassNotFoundException].not
You need to write instead:
a must not(throwA[ClassNotFoundException])
Even if it does not answer to your issue, you do not have to test if no exception is thrown. In this case, you'd better check if the intended result is ok... As soon as the test is executed, it means that it does not throw an exception.
The test below passes, if the the expression throws anything except for ClassNotFoundException
:
must throwA[Exception].like {
case m: ClassNotFoundException => false
case _ => true}
If you simply want to make sure that the expression doesn't throw ClassNotFoundException, why not just using try-catch block:
try{
...
}catch{
case m: ClassNotFoundException => fail("ClassNotFoundException")
case e => e.printStackTrace
}
How about something like this:
"An isSpaceNode function" should {
"not fail with a Group" in {
Group(<a/><b/>).isSpaceNode must not throwA(new UnsupportedOperationException)
}
}
精彩评论