Java Assert.assertEquals案例詳解

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);
}

到此這篇關於Java Assert.assertEquals案例詳解的文章就介紹到這瞭,更多相關Java Assert.assertEquals內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: