[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: Re: [groovy-user] shouldFail and assert against the exception object
From: Sergey Bondarenko <enterit () gmail ! com>
Date: 2009-05-26 15:54:13
Message-ID: 22ac04340905260854h59f96e25we1e3651bc2c5eb4f () mail ! gmail ! com
[Download RAW message or body]
i do not know why default behaviour was chosen to return exception message.
You could implement your own shouldFail, something like this:
def shouldFail(Class<Exception> expectedExceptionClass = Exception, Closure
closure) {
try {
closure()
} catch(e) {
if (!expectedExceptionClass.isInstance(e)) {
throw new AssertionError("Expected exception
$expectedExceptionClass was not thrown")
}
return e
}
throw new AssertionError("Expected exception $expectedExceptionClass was
not thrown")
}
def throwNPE = {null.dosomething()}
def throwNPEWithMessage = {throw new NullPointerException('some message')}
assert shouldFail{throwNPE()} instanceof NullPointerException
assert shouldFail{throwNPEWithMessage()}.message == 'some message'
shouldFail(IOException){throwNPE()} //fails with java.lang.AssertionError:
Expected exception class java.io.IOException was not thrown
Regards,
Sergey
2009/5/26 Helmut Denk <helmut.denk@gmx.de>
> SchemaValidationException
[Attachment #3 (text/html)]
<div>i do not know why default behaviour was chosen to return exception \
message.</div>You could implement your own shouldFail, something like \
this:<div><br><div><div><div>def shouldFail(Class<Exception> \
expectedExceptionClass = Exception, Closure closure) {</div> <div> try \
{</div><div> closure()</div><div> } catch(e) {</div><div> \
if (!expectedExceptionClass.isInstance(e)) {</div><div> throw new \
AssertionError("Expected exception $expectedExceptionClass was not \
thrown")</div> <div> }</div><div> return e</div><div> \
}</div><div> throw new AssertionError("Expected exception \
$expectedExceptionClass was not \
thrown")</div><div>}</div><div><br></div><div>def throwNPE = \
{null.dosomething()}</div> <div>def throwNPEWithMessage = {throw new \
NullPointerException('some message')}</div><div><br></div><div>assert \
shouldFail{throwNPE()} instanceof NullPointerException</div><div>assert \
shouldFail{throwNPEWithMessage()}.message == 'some message'</div> \
<div>shouldFail(IOException){throwNPE()} //fails with java.lang.AssertionError: \
Expected exception class java.io.IOException was not \
thrown</div><div><br></div><div><br></div><div>Regards,</div><div>Sergey</div><div><br></div>
</div></div><div><br><div class="gmail_quote">2009/5/26 Helmut Denk <span \
dir="ltr"><<a href="mailto:helmut.denk@gmx.de">helmut.denk@gmx.de</a>></span><br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex;"> SchemaValidationException \
</blockquote></div><br></div></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic