The difference between the Assert in Java and groovy

Source: Internet
Author: User
Tags assert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.