Assert.assertEquals()方法參數(shù)詳解
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類型. 注釋說, 會拋異常, 但這里沒有. 沒關系, 我們接著看里面調(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);
按照源碼分析, 我們可以把一個預期結果作為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);
}
到此這篇關于Assert.assertEquals()方法參數(shù)詳解的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android使用Jni實現(xiàn)壓力鍋數(shù)據(jù)檢測效果示例
這篇文章主要介紹了Android使用Jni實現(xiàn)壓力鍋數(shù)據(jù)檢測效果,涉及Android結合Jni實現(xiàn)進度條模擬壓力鍋數(shù)據(jù)監(jiān)測效果的相關操作技巧,需要的朋友可以參考下2017-12-12
Android中RecyclerView嵌套滑動沖突解決的代碼片段
這篇文章主要為大家詳細介紹了Android中RecyclerView嵌套滑動沖突解決的代碼片段,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

