網(wǎng)站首頁 編程語言 正文
junit.framework包下的Assert提供了多個斷言方法. 主用于比較測試傳遞進去的兩個參數(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參傳遞進去. 2參傳遞我們需要測試的方法. 然后執(zhí)行. 相等, 代碼繼續(xù)往下執(zhí)行, 不相等, 中斷執(zhí)行, 拋出異常信息!!!
略作一提:
Assert.assertSame(Object expected, Object actual)方法:
其比較的是引用地址是否相等, 并沒有對內(nèi)容進行比較:
/** * 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); }
原文鏈接:https://blog.csdn.net/tgvincent/article/details/81296349
相關(guān)推薦
- 2022-12-15 Go字典使用詳解_Golang
- 2022-09-14 Python實現(xiàn)刪除windows下的長路徑文件_python
- 2023-07-10 Gateway服務(wù)網(wǎng)關(guān)
- 2022-10-14 基于docker容器的gitlab遷移與數(shù)據(jù)恢復(fù)
- 2022-10-23 Android?Activity?View加載與繪制流程深入刨析源碼_Android
- 2023-05-29 Python中Merge使用的示例詳解_python
- 2022-08-14 C++學(xué)習(xí)之算術(shù)運算符使用詳解_C 語言
- 2022-07-11 pandas如何計算同比環(huán)比增長_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支