Assert是断言的意思,可以理解为“猜测”,如果猜测错误,则抛出java.lang.AssertionError异常。
引入jar包
import static org.junit.Assert.*; //第一种方式
import org.junit.Assert; //第二种方式
让测试直接出错,抛出 AssertionError 。(Java.lang.AssertionError)
让测试直接出错,并在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 object 为 null,如果不为 null ,抛出 AssertionError 。
猜测 object 为 null。
如果不为 null ,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 object 不为 null,如果为 null ,抛出 AssertionError 。
猜测 object 不为 null。
如果为 null ,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 expected 对象和 actual 对象引用的是同一个对象。
如果不同,抛出 AssertionError 。
猜测 expected 对象和 actual 对象引用的是同一个对象。 如果不同,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 expected 对象和 actual 对象引用不同的对象。 如果相同,抛出 AssertionError 。
猜测 expected 对象和 actual 对象引用不同的对象。 如果相同,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 condition 为 true 。 如果为 false ,抛出 AssertionError 。
猜测 condition 为 true 。 如果为 false , 在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测 condition 为 false 。 如果为 true , 抛出 AssertionError 。
猜测 condiiton 为 false 。 如果为 true , 在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个 long 类型 expected 和 actual 的值相等。 如果不相等,抛出 AssertionError 。
猜测两个 long 类型 expected 和 actual 的值相等。 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个 long 类型 unexpected 和 actual 的值不相等。 如果相等,抛出 AssertionError 。
猜测两个 long 类型 expected 和 actual 的值不相等。 如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个对象相等。 如果不相等,抛出 AssertionError 。
猜测两个对象相等。 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
注 ---- assertSame()和assertEquals()的区别见参考文章:
猜测两个对象不相等。 如果相等,抛出 AssertionError 。
猜测两个对象不相等。 如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个 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
猜测两个 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 !
猜测两个 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
猜测两个 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 !
猜测两个 long 类型 expected 和 actual 在 delta 偏差值下相等 如果不相等,抛出 AssertionError 。
猜测两个 long 类型 expect 和 actual 在 delta 偏差值下相等 如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个 long 类型 unexpected 和 actual 在 delta 偏差值下不相等 如果相等,抛出 AssertionError 。
猜测两个 long 类型 expect 和 actual 在 delta 偏差值下不相等 如果相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
猜测两个相同类型的数组的元素一一对应相等。
如果不相等,抛出 AssertionError 。
T 支持的类型有 int、byte、char、long、short、boolean、Object
猜测两个相同类型的数组的元素一一对应相等。
如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
T 支持的类型有 int、byte、char、long、short、boolean、Object
猜测两个相同类型的数组的元素,在 delte 偏差值下一一对应相等。
如果不相等,抛出 AssertionError 。
T 支持的类型有 float、double
猜测两个相同类型的数组的元素,在 delte 偏差值下一一对应相等。
如果不相等,在抛出 AssertionError 时输出 message 作为错误提示信息。
T 支持的类型有 float、double
猜测 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.*
猜测 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 : ...
原文:https://www.cnblogs.com/helloxiaojie/p/14916823.html