There is a Negative Groovy Assert with Message like:
def name = "John"
assert name == "Peter" : "Name should be John"
Which gives output:
Caught: java.lang.AssertionError: Name should be John. Expression: (name == Peter). Values: name = John
java.lang.AssertionError: Name should be John. Expression: (name == Peter). Values: name = John
But if the statement is true
there is no information in the log. So when you (or your coworker) checks the log later (coworker does not know the checks you implemented) you have no idea about assertions which had place.
So I'd like to log positive assertions. Something like (adding positive message ? "Name is " + name
):
def name = "Peter"
assert name == "Peter" : "Name should be John" ? "Name is " + name
Does it exists? I know I can log message after the assertion e.g.: log("Assert is correct: Name is " + name)
but I'd like to do it in one line assertion.
There is no such thing like positive assertion message, because it does not make much sense in general. Let's consider example you have posted:
assert name == "Peter" : "Name should be John" ? "Name is " + name
When expression name == "Peter"
evaluates to false
it is straightforward what happens - java.lang.AssertionError
is thrown with message Name should be John
. Positive branch in this case is not straightforward - where does the message Name is ${name}
should be log to? Using simple println
or maybe using slf4j's log.info()
or log.debug
? Where is explicit decision about that? And what if I don't want to have such information in my logs, because it only produces overhead?
Of course you could define your own method like:
void doAssert(boolean expr1, String errorMessage, Closure<Void> onSuccess = null) {
assert expr : errorMessage
onSuccess?.call()
}
and then do:
doAssert name == "Peter", "Name should be John", {
log.info("Name is ${name}")
}
but in long term this might be an overkill - you create a closure object only for some debugging information. You can always modify this method and comment out onSuccess?.call()
to limit some overhead created while calling a closure, although closure's object (and related Java anonymous class) gets created anyway.
But I guess you see that above method requires exactly same amount of effort as:
assert name == "Peter" : "Name should be John"
log.info("Name is ${name}")
I would rather suggest to use assertions as they were designed and don't produce much overhead. You can always log important information, but it has to be something that makes sense for maintainers. I don't see much value in getting informed that specific assertion was satisfied - I know that because program continued computations. Hope it helps you making right decision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With