这篇文章将为大家详细讲解有关Java中Assert.assertEquals的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
junit.framework包下的Assert提供了多个断言方法. 主用于比较测试传递进去的两个参数.
Assert.assertEquals);及其重载方法: 1. 如果两者一致, 程序继续往下运行. 2. 如果两者不一致, 中断测试方法, 抛出异常信息 AssertionFailedError .
查看源码, 以Assert.assertEqualsint expected, int actual)为例:
/** * Asserts that two ints are equal. 断言两个int是相等的 */ static public void assertEqualsint expected, int actual) { assertEqualsnull, expected, actual); }
可以看到里面调用了assertEqualsString 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 assertEqualsString message, int expected, int actual) { assertEqualsmessage, Integer.valueOfexpected), Integer.valueOfactual)); }
可以看到, 这里把int类型封箱成为Integer类型. 注释说, 会抛异常, 但这里没有. 没关系, 我们接着看里面调用: assertEqualsString 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 assertEqualsString message, Object expected, Object actual) { if expected == null && actual == null) { return; } if expected != null && expected.equalsactual)) { return; } failNotEqualsmessage, expected, actual); }
两个if语句, 判断了两者相等的情况: 引用地址)相等或者内容相等. 如果这两种if情况都不命中, 那么表明1参和2参实际是不相等, 所以代码会往下执行failNotEqualsString message, Object expected, Object actual)方法,并在此方法中抛出异常, 接下来就比较简单了:
static public void failNotEqualsString message, Object expected, Object actual) { failformatmessage, expected, actual)); } public static String formatString 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 failString message) { if message == null) { throw new AssertionFailedError); } throw new AssertionFailedErrormessage); }
以上可以看出, 最终是由failString message)这个方法抛出异常信息!!
Assert.assertEquals)使用方法:
使用, 示例代码:
Assert.assertEqualstrue, arry.contains"hello")); Assert.assertEquals39991L, aa.getLong"key3", 0L)); Assert.assertEqualstrue, bb.getBoolean"key4", false)); Assert.assertEquals5.3f, cc.getFloat"key5", 0.f)); Assert.assertEquals99, dd.getInt"key6", 1)); Assert.assertEquals"如果打印本信息, 证明参数不相等", 10L, 10);
按照源码分析, 我们可以把一个预期结果作为1参传递进去. 2参传递我们需要测试的方法. 然后执行. 相等, 代码继续往下执行, 不相等, 中断执行, 抛出异常信息!!!
略作一提:
Assert.assertSameObject 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 assertSameObject expected, Object actual) { assertSamenull, 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 assertSameString message, Object expected, Object actual) { if expected == actual) { return; } failNotSamemessage, expected, actual); }