The basic use of spring's assertion tool class assert

Source: Internet
Author: User
Tags assert object object

These days more busy looking at the previous project, found this spring under the Assert method, (previously used, but it seems to forget almost the * *)

Org.springframework.util.Assert;

The Assert assertion tool class, typically used for data legitimacy checks.

It's usually written like this in the usual judgment.

if (message== null | | message.equls ("")) {
throw new IllegalArgumentException ("Incorrect input information!");
}

The code above with the Assert tool class can be simplified to:

Assert.hastext (Message, "Input information Error!");


Here's a look at the common assertion methods in the Assert class that I collected:

Assert.notnull (Object object, "object is required")-Object non-empty
Assert.istrue (Object object, "object must be True")-the object must be True
Assert.notempty (Collection Collection, "Collection must not being empty")-collection non-empty
Assert.haslength (String text, "text must be specified")-the character is not null and the character length is not 0
Assert.hastext (String text, "text must not is empty")-text is not null and must contain at least one character that is not a space
Assert.isinstanceof (class Clazz, Object obj, "clazz must be of type [Clazz]")-obj must be able to be properly styled as a clazz-specified class

The basic use of spring's assertion tool class assert

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.