org.junit.Assert(斷言)


 

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 : ...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM