closures - what is shouldFail in groovy? -
how code within 'shouldfail?' block work? understand closure, code gets called regardless of whether or not call using signature. besides, what's deal "readonlypropertyexception" showing in parenthesis? if parameter, not setup listed in official documentation!!
questions: 'shouldfail'? how should invoked? how handle exception purportedly thrown method/function/closure?
void test02_readonlyfieldingroovybean() { // you've noticed how groovy automatically generates getters/setters you. if don't // want generate setter because it's read-only field? mark 'final'. groovy understand. // try modify ken's ssn. should readonlypropertyexception. def person = new groovyperson('ken', 'kousen', '7878') def failed = false shouldfail (readonlypropertyexception) { // ------------ start editing here ---------------------- system.out.println(" in should fail") person.ssn='8332'; // ------------ stop editing here ---------------------- failed = false system.out.println(" exiting should fail") } //def foobar=shouldfail("hjh"); //def foobar=true; failed=shouldfail('abc'); //system.out.println("failed: "+failed); assert failed // code wrapping additions verifies readonlyproperty exception has been thrown. // curly brackets ({}) represent closure. we'll means soon. }
shouldfail()
(in variant) takes class , closure. runs closure , reports test failure if closure not exit throwing exception of type. catching exception, don't - shouldfail()
you.
(reading comments , code around them, looks unit test should pass because setting .ssn
property of groovyperson
fail read-only property, causing readonlypropertyexception
.)
Comments
Post a Comment