Java Assert.assertEquals案例詳解
junit.framework包下的Assert提供了多個斷言方法. 主用于比較測試傳遞進(jìn)去的兩個參數(shù).
Assert.assertEquals();及其重載方法: 1. 如果兩者一致, 程序繼續(xù)往下運行. 2. 如果兩者不一致, 中斷測試方法, 拋出異常信息 AssertionFailedError .
查看源碼, 以Assert.assertEquals(int expected, int actual)為例:
/** * Asserts that two ints are equal. 斷言兩個int是相等的 */ static public void assertEquals(int expected, int actual) { assertEquals(null, expected, actual); }
可以看到里面調(diào)用了assertEquals(String message, int expected, int actual)方法:
/** * Asserts that two ints are equal. If they are not * an AssertionFailedError is thrown with the given message. * 如果不拋出帶有 message 的異常(AssertionFailedError)信息, 則表明兩者相等 */ static public void assertEquals(String message, int expected, int actual) { assertEquals(message, Integer.valueOf(expected), Integer.valueOf(actual)); }
可以看到, 這里把int類型封箱成為Integer類型. 注釋說, 會拋異常, 但這里沒有. 沒關(guān)系, 我們接著看里面調(diào)用: assertEquals(String message, Object expected, Object actual)方法:
/** * Asserts that two objects are equal. If they are not * an AssertionFailedError is thrown with the given message. * 如果不拋出帶有 message 的異常(AssertionFailedError)信息, 則表明兩者相等(這里比較的是Object對象) */ static public void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) { return; } if (expected != null && expected.equals(actual)) { return; } failNotEquals(message, expected, actual); }
兩個if語句, 判斷了兩者相等的情況: 引用(地址)相等或者內(nèi)容相等. 如果這兩種if情況都不命中, 那么表明1參和2參實際是不相等, 所以代碼會往下執(zhí)行failNotEquals(String message, Object expected, Object actual)方法,并在此方法中拋出異常, 接下來就比較簡單了:
static public void failNotEquals(String message, Object expected, Object actual) { fail(format(message, expected, actual)); } public static String format(String message, Object expected, Object actual) { String formatted = ""; if (message != null && message.length() > 0) { formatted = message + " "; } return formatted + "expected:<" + expected + "> but was:<" + actual + ">"; } /** * Fails a test with the given message. */ static public void fail(String message) { if (message == null) { throw new AssertionFailedError(); } throw new AssertionFailedError(message); }
以上可以看出, 最終是由fail(String message)這個方法拋出異常信息!!
Assert.assertEquals()使用方法:
使用, 示例代碼:
Assert.assertEquals(true, arry.contains("hello")); Assert.assertEquals(39991L, aa.getLong("key3", 0L)); Assert.assertEquals(true, bb.getBoolean("key4", false)); Assert.assertEquals(5.3f, cc.getFloat("key5", 0.f)); Assert.assertEquals(99, dd.getInt("key6", 1)); Assert.assertEquals("如果打印本信息, 證明參數(shù)不相等", 10L, 10);
按照源碼分析, 我們可以把一個預(yù)期結(jié)果作為1參傳遞進(jìn)去. 2參傳遞我們需要測試的方法. 然后執(zhí)行. 相等, 代碼繼續(xù)往下執(zhí)行, 不相等, 中斷執(zhí)行, 拋出異常信息!!!
略作一提:
Assert.assertSame(Object expected, Object actual)方法:
查看源碼, 其比較的是引用地址是否相等, 并沒有對內(nèi)容進(jìn)行比較:
/** * Asserts that two objects refer to the same object. If they are not * the same an AssertionFailedError is thrown. */ static public void assertSame(Object expected, Object actual) { assertSame(null, expected, actual); } /** * Asserts that two objects refer to the same object. If they are not * an AssertionFailedError is thrown with the given message. */ static public void assertSame(String message, Object expected, Object actual) { if (expected == actual) { return; } failNotSame(message, expected, actual); }
到此這篇關(guān)于Java Assert.assertEquals案例詳解的文章就介紹到這了,更多相關(guān)Java Assert.assertEquals內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線程之wait(),notify(),notifyAll()的詳解分析
本篇文章是對java多線程 wait(),notify(),notifyAll()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06IDEA創(chuàng)建Java項目導(dǎo)出Jar包運行
這篇文章主要介紹了IDEA創(chuàng)建Java項目導(dǎo)出Jar包運行,需要的朋友可以參考下2021-01-01淺談SpringBoot在使用測試的時候是否需要@RunWith
本文主要介紹了淺談SpringBoot在使用測試的時候是否需要@RunWith,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java中實體類為什么要實現(xiàn)Serializable序列化的作用
這篇文章主要介紹了Java中實體類為什么要實現(xiàn)Serializable序列化的作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java 重載(overload)與重寫(override)詳解及實例
這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實例的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-10-10在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明
這篇文章主要介紹了在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10