美文网首页
Assert工具类

Assert工具类

作者: 十二月的柒 | 来源:发表于2017-10-24 12:56 被阅读0次

org.springframework.util.Assert工具类

1.介绍

Assert :“断言”,它断定某一个实际的运行值和预期想一样,否则就抛出异常。

2.可用于简化代码

数据合法性检查:

if (message== null || message.equls("")) {

throw new IllegalArgumentException("输入信息错误!");

}

使用Assert简化后:

Assert.hasText((message, "输入信息错误!");

3.常用API介绍

Assert.notNull(Object object, "object is required")    -    对象非空

Assert.isTrue(Object object, "object must be true")   -    对象必须为true

Assert.notEmpty(Collection collection, "collection must not be empty")    -    集合非空

Assert.hasLength(String text, "text must be specified")   -    字符不为null且字符长度不为0

Assert.hasText(String text, "text must not be empty")    -     text 不为null且必须至少包含一个非空格的字符

Assert.isInstanceOf(Class clazz, Object obj, "clazz must be of type [clazz]")    -    obj必须能被正确造型成为clazz 指定的类

4.API链接:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/Assert.html

相关文章

  • Assert工具类

    org.springframework.util.Assert工具类 1.介绍 Assert :“断言”,它断定某...

  • Spring之工具类Assert

    首先,Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回。类似的,当我们...

  • SpringBoot内置工具类之 断言 Assert

    说到断言Assert,我们在查看源码时经常看到,它是使用比较频繁的一个工具类,但我也经常忽略它的存在。现在就让我们...

  • NodeJS 官方文档v5.3.0 学习笔记

    https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...

  • python之assert

    什么是assert python的assert是一个debug的工具,主要用于测试一个条件是否满足。条件满足:类似...

  • spring的断言工具类Assert的基本使用

    Assert(断言) Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回...

  • spring Assert工具使用

    方法入参检测工具类 Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回。...

  • 【Python】Debug 工具

    Debug debug的工具主要有print, log, assert, traceback, Raise Err...

  • Java的53个关键字

    abstract 表明类或者成员方法具有抽象属性 assert 用来进行程序调试 boolean 基本数据类...

  • TestNG断言

    TestNG中的Assertion,也是断言。断言是测试中最难写的部分。 Assert类(硬断言) 断言类是Ass...

网友评论

      本文标题:Assert工具类

      本文链接:https://www.haomeiwen.com/subject/swmtpxtx.html