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内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!