網站首頁 編程語言 正文
junit.framework包下的Assert提供了多個斷言方法. 主用于比較測試傳遞進去的兩個參數
.
Assert.assertEquals();及其重載方法:
- 1. 如果兩者一致, 程序繼續往下運行.
- 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); }
可以看到里面調用了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類型. 注釋說, 會拋異常, 但這里沒有. 沒關系, 我們接著看里面調用: 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語句, 判斷了兩者相等的情況: 引用(地址)相等或者內容相等. 如果這兩種if情況都不命中, 那么表明1參和2參實際是不相等, 所以代碼會往下執行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("如果打印本信息, 證明參數不相等", 10L, 10);
按照源碼分析, 我們可以把一個預期結果作為1參傳遞進去. 2參傳遞我們需要測試的方法. 然后執行. 相等, 代碼繼續往下執行, 不相等, 中斷執行, 拋出異常信息!!!
略作一提:
Assert.assertSame(Object expected, Object actual)方法:
其比較的是引用地址是否相等, 并沒有對內容進行比較:
/** * 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
相關推薦
- 2022-05-13 修復Qt程序長時間運行控件停止刷新
- 2023-01-28 python元組的可變與不可變問題_python
- 2022-02-25 Servlet配置啟動級別loadOnStartup注意事項
- 2022-08-04 Android解決所有雙擊優化的問題_Android
- 2023-01-19 python中的字符串切割?maxsplit_python
- 2022-10-23 C#中的yield關鍵字詳解_C#教程
- 2023-07-04 ES聚合查詢+條件搜索的實現
- 2022-09-12 jQuery事件注冊的實現示范_jquery
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支