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)
上一篇 2022年7月13日 下午10:36
下一篇 2022年7月13日 下午10:36


相关推荐

  • GetTickCount函数

    GetTickCount函数DWORDWINAPIG void 获取从电脑开机后 开始计时的毫秒数 返回值以 32 位的双字类型 DWORD 存储 因此可以存储的最大值是 2 32 1 ms 约为 49 71 天 因此若系统运行时间超过 49 71 天时 这个数就会归 0 MSDN 中也明确的提到了 Retrievesthe

    2026年3月19日
    2
  • C语言中int转char型

    C语言中int转char型关于 C 语言中的 int 类型转成 char 类型直接进行强制类型转换 使用 printf 输出占位符为 c 如果这个 int 数刚好为 0 输出就成了空格 方法 charInttoCha intx charr char x 0 returnr 下面的代码是 16 进制转二进制的函数 voidmain intnumber i num1 num2 unsignedchar 9 printf npleaseinput 16 num

    2026年3月26日
    2
  • JAVA算法:回文字符串相关问题详解(回文字符串总结)

    JAVA算法:回文字符串相关问题详解(回文字符串总结)JAVA算法:回文字符串相关问题详解(回文字符串总结)Q1.编写一个工具方法判断给定的字符串是否为回文字符串例如:给定一个字符串“aabbaa”,判断该字符串是否为回文字符串。算法设计如下: /* *给定一个字符串,判断该字符串是否为一个回文字符串 *start表示需要判断的起始位置 *end表示需要判断的结束位置 */ publicstatic…

    2022年5月24日
    46
  • linux 更改文件读写权限_如何查看自己文件的权限

    linux 更改文件读写权限_如何查看自己文件的权限整理下Linux文件权限相关知识一、查看文件夹或文件的可读可写权限:ls-l文件夹解析“drwxrwxrwx”,这个权限说明一共10位。第一位代表文件类型,有两个数值:“d”和“-”,“d”代表目录,“-”代表非目录。后面9位可以拆分为3组来看,分别对应不同用户,2-4位代表所有者user的权限说明,5-7位代表组群group的权限说明,8-10位代表其他人

    2025年10月30日
    5
  • Java Annotation Processor注解处理器如何Debug

    Java Annotation Processor注解处理器如何Debugjava注解处理器

    2025年7月24日
    4
  • mknod 详解

    mknod 详解网上找了很多关于 mknod 的文章 但每一篇都有点不足 故我在这里整合了一篇如下 下文转自 http fengjixuchui blog 51cto com nbsp nbsp nbsp 创建特殊文件 nbsp mknod options name b c majorminor 创建 FIFO 已命名的管道 mknod options name

    2026年3月17日
    2

发表回复

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

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