Assertions are used to verify that a hypothetical condition is true, and in groovy's assertion, if the hypothetical condition is not true, a Java.lang.AssertionError exception is thrown. Use the groovy expression to test the hypothetical condition.
Such as
assert 1==2: "One isn ' t"
Will throw an exception:
Exception thrownjava.lang.AssertionError:One isn' t. Expression: (1 = = 2) at Consolescript19.run (ConsoleScript19:1)
The Java assertion is also written like this:
Assert 1==2: "One isn ' t";
As can be seen, groovy's assertion syntax is the same as Java's assertion, and groovy's assertion omits semicolons.
Tips for using assertions:
When you use assertions, your app contains a message. This message can help others maintain and understand your code and clarify your intentions.
2. Strong assertions
The assertion assert keyword of groovy is simply checking that the expression is true. If False, the assertion simply tells the developer that the expression is not valid, and that the value of the variable used in the expression is nothing else. If a strong assertion is used, the output of the assertion provides a visual representation of each of the expression's sub-expressions. I'll explain the strong assertions in detail below.
assert New File (' HelloWorld.txt ') = =new file (' Hello.txt ')
Assertions make it easy to test your code.
The difference between the Assert in Java and groovy