Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Groovy: Assertions


Assertions are used to check the preconditions.

Syntax
assert [left expression] == [right expression] : (optional message)

If assertion succeeds, the nothing will happen. But if assertion fails, then Groovy provides information about each sub-expressions of the expression being asserted.

HelloWorld.groovy
assert true

int x = 1
assert x == 1

int y = 2
assert y == 1 : "y is not equal to 1" //This assertion fails

When you can above application, you can see below messages in console.

Caught: java.lang.AssertionError: y is not equal to 1. Expression: (y == 1). Values: y = 2
java.lang.AssertionError: y is not equal to 1. Expression: (y == 1). Values: y = 2
        at HelloWorld.run(HelloWorld.groovy:7)

If you omit the custom error message, then groovy generates the default message to the console.



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Groovy: Assertions

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×