Forum Discussion
You can add your own message than default one using below approach.
assert condition, customMessage
Example:
def x = -5
assert x > 0, "x should be positive"
Not sure how this answers my question. The problem is that I try to avoid extra work to assert a variable having to do anything. Groovy seems to be strange that it treats the original variable different when it is copied into another. As you can see from the examples I have given, it is both times the exact same test. The only difference that when I use the original variable for assert, it also displays a lot of junk after it, but when I copy it into another variable first and then do an assert, it doesn't. I just wonder if there is something that can be done to stop this behaviour.
- zht29707814 years agoNew Contributor
OH... another question or limitation. I just tested for JSONPath Match Assertion, if I use "*" on a single property that does not even exist, the test still passes.... how can I assert a dynamic property is some sort of number or at least not null?
Picture attached, I want to make sure the matching nested object has a number property called id and it can not be null. Its value is auto-generated so it is dynamic.
Hey zht2970781
you can use regex (I'm on the wrong machine right now) so I cant give you the exact name of the assertion, but there's an assertion that supports regex which would allow you to assert an attribute value is of numeric type
you can also do a script assertion using regex as well
as to your scenario of verifying both is numeric and is not null you could do something like the following in a script assertion:
def response = context.expand( '${REST Request#Response}' )
def numericAttribute = parse(response).read('$.id.numericAttribute')
log.info numericAttribute
assert (numericAttribute != null) && (numericAttribute ==~ /[0-9{3}/)
so hopefully (if I've got it right - there might be a mistake in there - I was rushing and going off memory, but hopefully it'll give you a steer) - the above assertion will parse the numericAttribute from the response (with JSONPath $.id.numericAttribute) and assert the attribute is not null and the value is made up of only 3 numbers. think you can use a \d to represent numerics here too - but I'd have to google to be sure and Im a bit pressed for time today.
hope the above helps,
Cheers,
Rich