public static Throwable shouldFailWithCause(Class clazz,Closure code){
  Throwable th=null;
  Throwable orig=null;
  int level=0;
  try {
    code.call();
  }
 catch (  GroovyRuntimeException gre) {
    orig=ScriptBytecodeAdapter.unwrap(gre);
    th=orig.getCause();
  }
catch (  Throwable e) {
    orig=e;
    th=orig.getCause();
  }
  while (th != null && !clazz.isInstance(th) && th != th.getCause() && level < MAX_NESTED_EXCEPTIONS) {
    th=th.getCause();
    level++;
  }
  if (orig == null) {
    fail("Closure " + code + " should have failed with an exception caused by type "+ clazz.getName());
  }
 else   if (th == null || !clazz.isInstance(th)) {
    fail("Closure " + code + " should have failed with an exception caused by type "+ clazz.getName()+ ", instead found these Exceptions:\n"+ buildExceptionList(orig));
  }
  return th;
}

public static Throwable shouldFailWithCause ( Class clazz , Closure code ) { Throwable th = null ; Throwable orig = null ; int level = 0 ; try { code . call ( ) ; } catch ( GroovyRuntimeException gre ) { orig = ScriptBytecodeAdapter . unwrap ( gre ) ; th = orig . getCause ( ) ; } catch ( Throwable e ) { orig = e ; th = orig . getCause ( ) ; } while ( th != null && ! clazz . isInstance ( th ) && th != th . getCause ( ) && level < MAX_NESTED_EXCEPTIONS ) { th = th . getCause ( ) ; level ++ ; } if ( orig == null ) { fail ( "Closure " + code + " should have failed with an exception caused by type " + clazz . getName ( ) ) ; } else if ( th == null || ! clazz . isInstance ( th ) ) { fail ( "Closure " + code + " should have failed with an exception caused by type " + clazz . getName ( ) + ", instead found these Exceptions:\n" + buildExceptionList ( orig ) ) ; } return th ; }
asserts that the given code closure fails when it is evaluated and that a particular exception can be attributed to the cause . the expected exception class is compared recursively with any nested exceptions using getcause ( ) until either a match is found or no more nested exceptions exist . < p > if a match is found the error message associated with the matching exception is returned . if no match was found the method will fail .
