Assert(断言)中的所有方法都会抛出不合法参数异常:
throw new IllegalStateException(message);
当然我们如果在业务逻辑中可以提前预知if else的情况,就不需要用到这个。
业务错误,我们有对应的业务处理方法(记录日志,返回报文,返回错误信息)
当某个架构逻辑中缺少某些必须的参数的时候可以用assert断言。
下面是常用‘Assert的方法,(都会报错java.lang.IllegalArgumentException)
@SpringBootTest(classes = HportalApplication.class) @RunWith(SpringRunner.class) public class AssertTest { @Test public void testNotNull() { String a = null; Assert.notNull(a, "a is null"); } @Test public void testIsTrue() { boolean a = false; Assert.isTrue(a, "a is false"); } @Test public void testNotEmpty() { ArrayList<String> list = new ArrayList<>(); Assert.notEmpty(list, "list is empty"); } @Test public void testHasLength() { String a = ""; String a1 = null; //Assert.hasLength(a, "a is blank"); Assert.hasLength(a1, "a1 is null"); } @Test public void testHasText() { String a = ""; String a1 = " "; String a2 = null; //Assert.hasText(a, "a is blank"); //Assert.hasText(a1, "a is blank kk"); Assert.hasText(a2, "a is null"); } }
原文:https://www.cnblogs.com/windy13/p/11808805.html