Assert.assertEquals作用「建议收藏」

Assert.assertEquals作用「建议收藏」junit.framework包下的Assert提供了多个断言方法.主用于比较测试传递进去的两个参数.Assert.assertEquals();及其重载方法:1.如果两者一致,程序继续往下运行.2.如果两者不一致,中断测试方法,抛出异常信息AssertionFailedError.查看源码,以Assert.assertEquals(intexpecte…

大家好,又见面了,我是你们的朋友全栈君。

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://javaforall.net/157855.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • redis的分布式解决方式–codis

    redis的分布式解决方式–codis

    2022年1月27日
    42
  • LoadLibrary失败的原因「建议收藏」

    LoadLibrary失败的原因「建议收藏」今天使用LoadLibrary时,失败,于是翻了一下MSDN:LoadLibraryTheLoadLibraryfunctionmapsthespecifiedexecutablemoduleintotheaddressspaceofthecallingprocess. Foradditionalloadoptions,usetheLo

    2022年7月13日
    26
  • sublime 4113 激活码【最新永久激活】

    (sublime 4113 激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html0UY7RF7AC5-eyJsaWNlbnNlSWQi…

    2022年3月28日
    403
  • 概率论中PDF、PMF和CDF的区别与联系

    概率论中PDF、PMF和CDF的区别与联系在概率论中,经常出现PDF、PMF和CDF,那么这三者有什么区别与联系呢?1.概念解释PDF:概率密度函数(probabilitydensityfunction),在数学中,连续型随机变量的概率密度函数(在不至于混淆时可以简称为密度函数)是一个描述这个随机变量的输出值,在某个确定的取值点附近的可能性的函数。PMF:概率质量函数(probabilitymassfunct…

    2022年5月24日
    59
  • ugui drawcall优化_DrawerLayout

    ugui drawcall优化_DrawerLayoutUGUIdrawcall合并原理高数量的drawcall带来的坏处不用多说了,本篇重点说的是UGUI是如何合并drawcall的。通过这篇博客,你将学会如何精算一个UGUI界面到底有几个drawcall,并且能想象出各UI控件的渲染顺序(即FrameDebugger窗口里的渲染顺序)。概念篇在学习本篇之前,你需要了解以下几个名词。bottomUIA是B的botto…

    2026年1月26日
    5
  • POJO简介

    POJO简介POJO 一:什么是POJOPOJO的名称有多种,pureoldjavaobject、plainordinaryjavaobject等。按照MartinFowler的解释是“PlainOldJavaObject”,从字面上翻译为“纯洁老式的java对象”,但大家都使用“简单java对象”来称呼它。POJO的内在含义是指那些没有从任何类继承、也没有实现任何接口,更没有被其它框…

    2022年5月28日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号