org.junit.Assert
2022/6/20 23:20:20
本文主要是介绍org.junit.Assert,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
引入包,以下两种方式都是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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-01为什么公共事业机构会偏爱 TiDB :TiDB 数据库在某省妇幼健康管理系统的应用
- 2024-04-26敏捷开发:想要快速交付就必须舍弃产品质量?
- 2024-04-26静态代码分析的这些好处,我竟然都不知道?
- 2024-04-26你在测试金字塔的哪一层?(下)
- 2024-04-26快刀斩乱麻,DevOps让代码评审也自动起来
- 2024-04-262024年最好用的10款ER图神器!
- 2024-04-2203-为啥大模型LLM还没能完全替代你?
- 2024-04-2101-大语言模型发展
- 2024-04-17基于SpringWeb MultipartFile文件上传、下载功能
- 2024-04-14个人开发者,Spring Boot 项目如何部署