引入包,以下两种方式都是OK的,看个人喜好,我倾向于使用第二种,会更加清晰直观。下面的代码我都会用第二种

import static org.junit.Assert.*;
import org.junit.Assert;

Assert是断言的意思,我更喜欢理解为"猜测",比断言要顺口和通俗。如果猜测错误,则抛出java.lang.AssertionError异常

1 - Assert.fail()
让测试直接出错,抛出 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)
猜测 condiiton 为 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 作为错误提示信息。

注 ---- assertNotSame()和assertNotEquals()的区别与上面 19- 和 20- 的区别类似。

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 十分灵活,需要引入包import static org.hamcrest.Matchers.*;,更多使用可参考文档: 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. <p>在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法</p>

    在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法 1.assertEquals()方法,用来查看对象中存的值是否是期待的值,与字符串比較 ...

  2. org.junit.Assert(断言)

    org.junit.Assert(断言) Assert是断言的意思,可以理解为"猜测",如果猜测错误,则抛出java.lang.AssertionError异常.  引入jar包  ...

  3. JUnit Assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...

  4. JUnit之断言assert

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

  5. junit断言和junit注释assert

    JUnit - 使用断言 断言 所有的断言都包含在 Assert 类中 public class Assert extends java.lang.Object 这个类提供了很多有用的断言方法来编写测 ...

  6. 【JUnit】junit4的几个assert方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhtao01/article/details/27858225 在静态类junit.framewor ...

  7. Junit mockito解耦合测试

    Mock测试是单元测试的重要方法之一. 1.相关网址 官网:http://mockito.org/ 项目源码:https://github.com/mockito/mockito api:http:/ ...

  8. Junit的使用

    Junit是用于编写单元测试的框架.对于已经写好的函数,可以使用Junit生成单元测试代码. 自己的环境是:Linux Java环境是:JDK1.7 IDE:Eclipse Java EE IDE f ...

  9. Android 单元测试(junit、mockito、robolectric)

    1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...

  10. 学习Maven之Maven Surefire Plugin(JUnit篇)

    1.maven-surefire-plugin是个什么鬼? 如果你执行过mvn test或者执行其他maven命令时跑了测试用例,你就已经用过maven-surefire-plugin了.maven- ...

随机推荐

  1. Clickhouse副本及分片

    副本 副本的目的主要是保障数据的高可用性,即使一台 ClickHouse 节点宕机,那么也可以从其他服务器获得相同的数据 配置副本 1. zookeeper集群准备 2. Clickhouse准备两个 ...

  2. 根据docker镜像反推dockerfile

    Dockerfile 是一个文本文件,其中包含我们为了构建 Docker 镜像而手动执行的所有命令. Docker 可以从 Dockerfile 中读取指令来自动构建镜像.我们可以使用 docker ...

  3. 基于Java+SpringBoot+Mysql实现的快递柜寄取快递系统功能实现四

    一.前言介绍: 1.1 项目摘要 随着电子商务的迅猛发展和城市化进程的加快,快递业务量呈现出爆炸式增长的趋势.传统的快递寄取方式,如人工配送和定点领取,已经无法满足现代社会的快速.便捷需求.这些问题不 ...

  4. (Redis基础教程之十) 如何在Redis中运行事务

    介绍 Redis是一个开源的内存中键值数据存储.Redis允许您计划一系列命令,然后一个接一个地运行它们,这一过程称为_transaction_.每个事务都被视为不间断且隔离的操作,以确保数据完整性. ...

  5. Mac下常用软件汇总(精)

    1.连接Windows远程连接工具(Microsoft-Remote-Desktop-For-Mac ) 2.SSH管理工具:Royal TSX 下载地址:Royal Apps Royal TSX 是 ...

  6. CSS3 过渡和动画

    1.CSS过渡 含义:在没有过渡属性的时候,当一个元素的属性值发生变化时,浏览器就会将这个元素瞬间渲染成新属性值的样式.例如一个定位元素top:0,动态修改成top:100px,这个元素就瞬间跑到10 ...

  7. MySql 9 in Docker 利用克隆插件搭建主从

    环境说明 Docker Windows 11 MySql 9.1.0 搭建步骤 1. 准备主库 准备一个主库的配置文件 master.cnf [mysqld] server-id=1 log-bin= ...

  8. 教你自创工作流,赋予AI助理个性化推荐超能力

    之前,我们已经完成了工作流的基本流程和整体框架设计,接下来的任务就是进入实际操作和实现阶段.如果有同学对工作流的整体结构还不够熟悉,可以先参考一下这篇文章,帮助你更好地理解和掌握工作流的各个部分: 本 ...

  9. IO介绍-下

    中断 由外部设备引起的中断,称为外中断. 由内部错误引起的中断,称为内中断,或者是陷入.例如:非法指令,地址越界,电源故障等.   中断向量表 中断优先级 多中断源的处理方式 屏蔽中断 嵌套中断 根据 ...

  10. python+playwright安装+使用vsocde运行代码

    python虚拟环境 1.安装python,环境配置 2.修改pip镜像源 3.新增虚拟环境 注意路径,例子的路径是在python的目录下生成一个venv文件夹 进入venv文件夹,使用virtual ...