Assert.assertEquals()方法参数详解

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

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

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语句, 判断了两者相等的情况: 引用(地址)相等或者内容相等. 如果这两种情况都不相等, 那么表明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/158778.html原文链接:https://javaforall.net

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


相关推荐

  • f1 score java_F1 score「建议收藏」

    f1 score java_F1 score「建议收藏」项目中需要判断用户提交的多选题选项的正确率,比如正确答案应该为a,b,c,而用户选择的是a,d,那么如何判断他的正确率呢,这个场景就需要用到F1score来计算。FromWikipedia,thefreeencyclopediahttp://en.wikipedia.org/wiki/F1_scoreInstatisticalanalysisofBinaryclassi…

    2022年10月15日
    0
  • 什么是幂等性?(幂等处理是什么意思)

    HTTP幂等方法,是指无论调用多少次都不会有不同结果的HTTP方法。不管你调用一次,还是调用一百次,一千次,结果都是相同的。HTTPGET方法HTTPGET方法,用于获取资源,不管调用多少次接口,结果都不会改变,所以是幂等的。GET/tickets#获取ticket列表GET/tickets/12#查看某个具体的ticket只…

    2022年4月17日
    99
  • linux系统nginx启动_电脑更新时重启电脑会怎样

    linux系统nginx启动_电脑更新时重启电脑会怎样Nginx是高性能的web服务器也是非常好用反向代理服务器,可以实现负载均衡,动静分离等策略,在linux下用的非常多。下面是下载地址http://nginx.org/en/download.html安装过程需要对Linux网络和配置yum源的知识比较熟悉下载下来并传入到服务器。第一步是进行解压tar-zxvfnginx-1.13.1

    2022年10月5日
    0
  • php实现微信小程序消息通知「建议收藏」

    php实现微信小程序消息通知「建议收藏」接入消息通知指引地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#%E6%A8%A1%E7%89%88%E6%B6%88%E6%81%AF%E7%AE%A1%E7%9…

    2022年9月15日
    0
  • 执行python程序的两种方式

    执行python程序的两种方式执行python程序的两种方式交互式python是高级(解释型)语言,写一句执行一句。命令行式python和python解释器是一种东西,我们说的打开python就是打开python解释器。

    2022年7月5日
    21
  • VC++ CString 与 int 类型转换「建议收藏」

     摘自:http://blog.csdn.net/a951084634/article/details/6961133 CString_temp="100";int_int;_int=atoi(_temp);======================================================================CSt…

    2022年4月6日
    36

发表回复

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

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