参考
环境
环境 | 版本 | 操作 |
---|---|---|
windows | 10 | |
JDK | 11 | |
Springboot | 2.3.12.RELEASE |
注意
引入的包为 import org.springframework.util.Assert;
介绍
对象和类型断言
函数 | 说明 |
---|---|
notNull() | 假设对象不null |
isNull() | 检查对象为null |
isInstanceOf() | 检查对象必须为另一个特定类型的实例 |
isAssignable() | 检查类型 |
文本断言
函数 | 说明 |
---|---|
hasLength() | 检查字符串不是空符串,意味着至少包含一个空白,可以使用hasLength()方法 |
hasText() | 增强检查条件,字符串至少包含一个非空白字符,可以使用hasText()方法 |
doesNotContain() | 检查参数不包含特定子串 |
逻辑断言
函数 | 说明 |
---|---|
isTrue() | 条件为假抛出IllegalArgumentException 异常 |
state() | 该方法与isTrue一样,但抛出IllegalStateException异常 |
Collection和map断言
函数 | 说明 |
---|---|
Collection应用notEmpty() | Collection不是null并包含至少一个元素 |
map应用notEmpty() | 检查map不null,并至少包含一个entry(key,value键值对) |
数组断言
函数 | 说明 |
---|---|
notEmpty() | 可以检查数组不null,且至少包括一个元素 |
noNullElements() | 确保数组不包含null元素 |
使用
package com.example.demo.controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class DemoController {
@GetMapping("demo")
public String demo() {
Assert.isTrue(false, "不应该为false");
return "";
}
}
- SpringBoot2 SpringBoot 单元 Assertspringboot2 springboot单元assert springboot2 springboot单元 springboot2 springboot2 springboot springboot2 springboot swagger3 swagger springboot2 springboot mybatis springboot2 springboot lettuce redis springboot2 springboot spring3 spring springboot2 springboot后台 管理系统 springboot2 springcache springboot