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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux配置ip的方法,linux下配置ip地址四种方法(图文方法)

    linux配置ip的方法,linux下配置ip地址四种方法(图文方法)主要是用第四种方法(1)Ifconfig命令第一种使用ifconfig命令配置网卡的ip地址。此命令通常用来零时的测试用,计算机启动后ip地址的配置将自动失效。具体用法如下。Ipconfigethxipaddnetmaskx.x.x.x。其中ethx中的x代表第几快以太网卡,默认第一块为0.ipadd代表ip地址。x.x.x..x为子网掩码。例如给网卡eth0配置的i…

    2022年5月2日
    52
  • linux开启校时服务,NTP校时服务

    linux开启校时服务,NTP校时服务NetworkTimeProtocol(NTP)是用来使计算机时间同步的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行同步,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可通过加密确认的方式来防止恶意攻击。学院NTP校时服务基于NTP网络协议搭建,为全校提供精准的校时服务,其默认使用UDP协议的123端口。Windows用户使用方法:…

    2022年6月29日
    27
  • PAD图_流程图转换成N—S图和PAD图

    PAD图_流程图转换成N—S图和PAD图地址链接:http://star.aust.edu.cn/~jjguan/guanjj/rjgc/chapter4/cha4_11_3_c1.htm

    2022年8月13日
    8
  • Apache服务器的配置[通俗易懂]

    Apache服务器的配置[通俗易懂]Apache服务器的配置

    2022年4月23日
    46
  • Vmware安装win10的失败总结

    Vmware安装win10的失败总结下了番茄,萝卜,深度等多个win10镜像均出现各种莫名其妙的小问题,还是linux大法好哇~~失败一:          未手动分区,许久不装windows系统了,忘记了手动分区,用镜像里自带的分区工具,如diskgenius等分区后安装失败二:          分区后需进入bios设置优先从光盘启动的模式失败三:          找不到ghost文件,进入winPE手动查找光盘里…

    2022年6月22日
    279
  • python 读json_python json文件

    python 读json_python json文件python中和json读写相关的主要是json模块的以下四个函数dumps()将一个python对象编码为json对象 loads()讲一个json对象解析为python对象 dump()将python对象写入文件 load()从文件中读取json数据1、dumps()和loads()主要用于Python和json对象的相互转化。importjsonprices…

    2022年10月12日
    1

发表回复

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

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