org.junit.Assert(断言)

Assert是断言的意思,可以理解为“猜测”,如果猜测错误,则抛出java.lang.AssertionError异常。

 引入jar包
 import static org.junit.Assert.*;   //第一种方式
 import org.junit.Assert;   //第二种方式
1、 Assert.fail ( )
 让测试直接出错,抛出 AssertionError 。(Java.lang.AssertionError)
2 、Assert.fail ( String message )
 让测试直接出错,并在抛出 AssertionError 时输出 message 作为错误提示信息。
3、Assert.assertNull ( Object object )
 猜测 object 为 null,如果不为 null ,抛出 AssertionError 。
4、Assert.assertNull ( String message, Object object )
 猜测 object 为 null。
 如果不为 null ,在抛出 AssertionError 时输出 message 作为错误提示信息。
5、Assert.assertNotNull ( Object object )
 猜测 object 不为 null,如果为 null ,抛出 AssertionError 。
6、Assert.assertNotNull(String message, Object object)
 猜测 object 不为 null。
 如果为 null ,在抛出 AssertionError 时输出 message 作为错误提示信息。
7、Assert.assertSame ( Object expected, Object actual )
 猜测 expected 对象和 actual 对象引用的是同一个对象。
 如果不同,抛出 AssertionError 。
8、Assert.assertSame ( String message, Object expected, Object actual )
猜测 expected 对象和 actual 对象引用的是同一个对象。
如果不同,在抛出 AssertionError 时输出 message 作为错误提示信息。
9、Assert.assertNotSame ( Object expected, Object actual )
猜测 expected 对象和 actual 对象引用不同的对象。
如果相同,抛出 AssertionError 。
10、Assert.assertNotSame ( String message, Object expected, Object actual )
猜测 expected 对象和 actual 对象引用不同的对象。
如果相同,在抛出 AssertionError 时输出 message 作为错误提示信息。
11、Assert.assertTrue ( boolean condition )
猜测 condition 为 true 。
如果为 false ,抛出 AssertionError 。
12、Assert.assertTrue ( String message, boolean condition )
猜测 condition 为 true 。
如果为 false , 在抛出 AssertionError 时输出 message 作为错误提示信息。
13、Assert.assertFalse ( boolean condition )
猜测 condition 为 false 。
如果为 true , 抛出 AssertionError 。
14、Assert.assertFalse ( String message, boolean condition )
猜测 condiiton 为 false 。
如果为 true , 在抛出 AssertionError 时输出 message 作为错误提示信息。
15、Assert.assertEquals ( long expected, long actual )
猜测两个 long 类型 expected 和 actual 的值相等。
如果不相等,抛出 AssertionError 。
16、Assert.assertEquals ( String message, long expected, long actual )
猜测两个 long 类型 expected 和 actual 的值相等。
如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
17、Assert.assertNotEquals ( long unexpected, long actual )
猜测两个 long 类型 unexpected 和 actual 的值不相等。
如果相等,抛出 AssertionError 。
18、Assert.assertNotEquals ( String message, long unexpected, long actual )
猜测两个 long 类型 expected 和 actual 的值不相等。
如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
19、Assert.assertEquals(Object expected, Object actual)
猜测两个对象相等。
如果不相等,抛出 AssertionError 。
20、Assert.assertEquals(String message, Object expected, Object actual)
猜测两个对象相等。
如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。

注 ---- assertSame()和assertEquals()的区别见参考文章:JUnit中assertEquals和assertSame方法的不同

21、Assert.assertNotEquals ( Object unexpected, Object actual )
猜测两个对象不相等。
如果相等,抛出 AssertionError 。
22、Assert.assertNotEquals ( String message, Object unexpected, Object actual )
猜测两个对象不相等。
如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
23、Assert.assertEquals ( float expected, float actual, float delta )
 猜测两个 float 类型 expect 和 actual 在 delta 偏差值下相等
 如果不相等,抛出 AssertionError 。
 //例1
 Assert.assertEquals(1.1, 1.3, 0.2)    //Pass
 //例2
 Assert.assertEquals(1.3, 1.1, 0.2)    //Pass
 //例3
 Assert.assertEquals(1.1, 1.3, 0.1)    //AssertionError
 //例4
 Assert.assertEquals(1.3, 1.1, 0.1)    //AssertionError
24、Assert.assertEquals ( String message, float expected, float actual, float delta )
 猜测两个 float 类型 expect 和 actual 在 delta 偏差值下相等
 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
 //例1
 Assert.assertEquals("Not equal !",1.1, 1.3, 0.2)    //Pass
 //例2
 Assert.assertEquals("Not equal !",1.3, 1.1, 0.2)    //Pass
 //例3
 Assert.assertEquals("Not equal !",1.1, 1.3, 0.1)    //AssertionError : Not equal !
 //例4
 Assert.assertEquals("Not equal !",1.3, 1.1, 0.1)    //AssertionError : Not equal !
25、Assert.assertNotEquals ( float unexpected, float actual, float delta )
 猜测两个 float 类型 unexpected 和 actual 在 delta 偏差值下不相等
 如果相等,抛出 AssertionError 。
 //例1
 Assert.assertNotEquals(1.1, 1.3, 0.1)    //Pass
 //例2
 Assert.assertNotEquals(1.3, 1.1, 0.1)    //Pass
 //例3
 Assert.assertNotEquals(1.1, 1.3, 0.2)    //AssertionError
 //例4
 Assert.assertNotEquals(1.3, 1.1, 0.2)    //AssertionError
26、Assert.assertNotEquals ( String message, float unexpected, float actual, float delta )
 猜测两个 float 类型 expect 和 actual 在 delta 偏差值下不相等
 如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
 //例1
 Assert.assertNotEquals("Equal !",1.1, 1.3, 0.1)    //Pass
 //例2
 Assert.assertNotEquals("Equal !",1.3, 1.1, 0.1)    //Pass
 //例3
 Assert.assertNotEquals("Equal !",1.1, 1.3, 0.2)    //AssertionError : Equal !
 //例4
 Assert.assertNotEquals("Equal !",1.3, 1.1, 0.2)    //AssertionError : Equal !
27、Assert.assertEquals ( long expected, long actual, long delta )
猜测两个 long 类型 expected 和 actual 在 delta 偏差值下相等
如果不相等,抛出 AssertionError 。
28、Assert.assertEquals ( String message, long expected, long actual, long delta )
猜测两个 long 类型 expect 和 actual 在 delta 偏差值下相等
如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
29、Assert.assertNotEquals ( long unexpected, long actual, long delta )
猜测两个 long 类型 unexpected 和 actual 在 delta 偏差值下不相等
如果相等,抛出 AssertionError 。
30、Assert.assertNotEquals ( String message, long unexpected, long actual, long delta )
猜测两个 long 类型 expect 和 actual 在 delta 偏差值下不相等
如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
31~37、Assert.assertArrayEquals ( T[] expected, T[] actual )
 猜测两个相同类型的数组的元素一一对应相等。
 如果不相等,抛出 AssertionError 。
 T 支持的类型有 int、byte、char、long、short、boolean、Object
38~44、Assert.assertArrayEquals ( String message, T[] expected, T[] actual )
 猜测两个相同类型的数组的元素一一对应相等。
 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
 T 支持的类型有 int、byte、char、long、short、boolean、Object
44~45、Assert.assertArrayEquals ( T[] expected, T[] actual, T delta )
 猜测两个相同类型的数组的元素,在 delte 偏差值下一一对应相等。
 如果不相等,抛出 AssertionError 。
 T 支持的类型有 float、double
46~47、Assert.assertArrayEquals ( String message, T[] expected, T[] actual, T delta )
 猜测两个相同类型的数组的元素,在 delte 偏差值下一一对应相等。
 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
 T 支持的类型有 float、double
48、Assert.assertThat ( T actual, Matcher<? super T> matcher )
 猜测 actual 的值符合规则 matcher。
 如果不符合,抛出 AssertionError 。
 //例1
 Assert.assertThat(1, is(1));               //Pass
 //例2
 Assert.assertThat(1, greaterThan(0));      //Pass
 //例3
 Assert.assertThat(1, is(0));               //AssertionError
 //例4
 Assert.assertThat(1, greaterThan(1));      //AssertionError

注:Matcher 十分灵活

参考文档: org.hamcrest.Matchers

 引入包
 import static org.hamcrest.Matchers.*
49、Assert.assertThat ( String reason, T actual, Matcher<? super T> matcher )
 猜测 actual 的值符合规则 matcher。
 如果不符合,在抛出 AssertionError 时输出 reason 作为错误提示信息。
 //例1
 Assert.assertThat("not match reason: ...", 1, is(1));          //Pass
 //例2
 Assert.assertThat("not match reason: ...",1, greaterThan(0));  //Pass
 //例3
 Assert.assertThat("not match reason: ...",1, is(0));           //AssertionError : not match reason : ...
 //例4
 Assert.assertThat("not match reason: ...",1, greaterThan(1));  //AssertionError : not match reason : ...

org.junit.Assert(断言)的更多相关文章

  1. JUnit之断言assert

    一.简介 JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活: 使用全新的断言语法:assertThat,结合Ham ...

  2. Spring Assert 断言

    Assert(断言)的初步理解构思 Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方法入参进行合 法性检查, ...

  3. Spring Framework 的 Assert断言

    知识共享才能传播,博采众家之长,才能推陈出新!-- 参考 https://www.cnblogs.com/hwaggLee/p/4778101.html 一.什么是 Assert(断言)? Web 应 ...

  4. java——assert(断言)方法

    包:org.junit.Assert; assertEqual(a,b,[msg='测试失败时打印的信息']): 断言a和b是否相等,相等则测试用例通过. assertNotEqual(a,b,[ms ...

  5. C++ ASSERT() 断言机制

    C++ ASSERT() 断言机制 ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE (0), 程序将报告错误,并终止执行.如果表达式不为0,则继 ...

  6. Idea中开启assert断言

    先打开相应的配置面板,有以下两种方式.   然后在VM栏里输入 -enableassertions 或者 -ea 就好了 然后编写程序试试 我的目录结构如下:(因为Main class那里要写类的全限 ...

  7. Python全栈开发之---assert断言

    一.python assert的作用: 根据Python 官方文档解释(https://docs.python.org/3/reference/simple_stmts.html#assert), & ...

  8. Python assert 断言函数

    http://blog.csdn.net/hunyxv/article/details/52737339 使用assert断言是学习python一个非常好的习惯,python assert 断言句语格 ...

  9. pytest自动化7:assert断言

    前言:assert断言就是将实际结果和期望结果做对比,符合预期结果就测试pass,不符合预期就测试failed. 实例1:简单断言 实例1优化版--增加异常信息文字描述 异常断言 excinfo 是一 ...

随机推荐

  1. 操作系统中的进程同步与Window中利用内核对象进行线程同步的关系

    操作系统中为了解决进程间同步问题提出了用信号量机制,信号量可分为四种类型分别是互斥型信号量,记录型信号量,AND型信号量,信号量集. 互斥型信号量 互斥型信号量是资源数量为1的特殊的记录型信号量.表示 ...

  2. c#log4net简单好用的配置

    新建文件log4net.config 编辑文件log4net.config <configuration> <configSections> <!--日志记录--> ...

  3. 图扑软件正式加入腾讯智维生态发展计划,智能 IDC 开启数字经济新征程

    4 月 23 日,主题为<智汇科技,维新至善>的腾讯数据中心智维技术研讨会在深圳胜利召开,发布了腾讯智维 2.0 技术体系,深度揭秘了智维 2.0 新产品战略和技术规划.图扑软件(High ...

  4. 北航OO(2020)第三单元博客作业

    一.JML理论基础及相关工具链 1.JML理论基础 该部分梳理本单元作业中涉及到的JML知识. 1.1注释结构 JML采用javadoc注释的方式来表示规格,且每行以@开头.通过使用//@annota ...

  5. Java集合详解(三):LinkedList原理解析

    概述 本文是基于jdk8_271源码进行分析的. LinkedList底层是基于链表实现.链表没有长度限制,内存地址不需要固定长度,也不需要是连续的地址来进行存储,只需要通过引用来关联前后元素即可完成 ...

  6. 【BIGDATA】Grafana告警之webhook的坑

    近日搭建一套基于ELK&Grafana的监控告警平台,目的是将生产端某性能日志导入ES中,通过Grafana进行可视化监测,同时设置告警. 告警内容推送到自建的webhook服务后,转发到指定 ...

  7. Linux下Shell实现服务器IP监测

    实验室有一个服务器放在机房,装的是Ubuntu Server,IP为自动分配,因此一旦IP有变化就无法远程操作,必须去机房记录新的IP.学了几天Shell之后想,是不是可以定时检测其IP的变化,一旦有 ...

  8. dmesg -w 查看硬件参数

    dmesg -w 查看硬件参数 14,笔记本硬件问题,使用dmesg -w可以看到,内核不断受到硬件过来的热插拔信号

  9. 分布式存储ceph---ceph osd 故障硬盘更换(6)

    正常状态: 故障状态: 实施更换步骤: 1.关闭ceph集群数据迁移: osd硬盘故障,状态变为down.在经过mod osd down out interval 设定的时间间隔后,ceph将其标记为 ...

  10. Linux进阶之磁盘管理及LVM逻辑卷

    本节内容 磁盘管理 LVM 一.磁盘管理 1.硬盘接口 种类及其应用: IDE接口硬盘多用于家用产品,部分应用于服务器 SATA SCSI接口硬盘主要应用于服务器 SAS只在高端服务器上,价格昂贵 2 ...