JavaAsrt.asrtEquals案例详解

更新时间:2023-06-15 19:02:49 阅读: 评论:0

JavaAsrt.asrtEquals案例详解
junit.framework包下的Asrt提供了多个断⾔⽅法. 主⽤于⽐较测试传递进去的两个参数.
Asrt.asrtEquals();及其重载⽅法: 1. 如果两者⼀致, 程序继续往下运⾏. 2. 如果两者不⼀致, 中断测试⽅法, 抛出异常信息AsrtionFailedError .
查看源码, 以Asrt.asrtEquals(int expected, int actual)为例:
/**
* Asrts that two ints are equal. 断⾔两个int是相等的
*/
static public void asrtEquals(int expected, int actual) {
asrtEquals(null, expected, actual);
}
可以看到⾥⾯调⽤了asrtEquals(String message, int expected, int actual)⽅法:
/**
* Asrts that two ints are equal. If they are not
* an AsrtionFailedError is thrown with the given message.
* 如果不抛出带有 message 的异常(AsrtionFailedError)信息, 则表明两者相等
*/
static public void asrtEquals(String message, int expected, int actual) {
asrtEquals(message, Integer.valueOf(expected), Integer.valueOf(actual));
}
可以看到, 这⾥把int类型封箱成为Integer类型. 注释说, 会抛异常, 但这⾥没有. 没关系, 我们接着看⾥⾯调⽤:
asrtEquals(String message, Object expected, Object actual)⽅法:
/**
行长英文* Asrts that two objects are equal. If they are not
* an AsrtionFailedError is thrown with the given message.
* 如果不抛出带有 message 的异常(AsrtionFailedError)信息, 则表明两者相等(这⾥⽐较的是Object对象)
*/
static public void asrtEquals(String message, Object expected, Object actual) {
if (expected == null && actual == null) {
return;gate
教学相长翻译
}
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));
reluctant}
public static String format(String message, Object expected, Object actual) {
String formatted = "";
demonhunterif (message != null && message.length() > 0) {
formatted = message + " ";
}
return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
46级查询成绩}
/**
* Fails a test with the given message.
*/
static public void fail(String message) {
if (message == null) {
throw new AsrtionFailedError();
}
throw new AsrtionFailedError(message);
}
以上可以看出, 最终是由fail(String message)这个⽅法抛出异常信息!!
Asrt.asrtEquals()使⽤⽅法:
使⽤, ⽰例代码:
节拍英文Asrt.asrtEquals(true, ains("hello"));
Asrt.asrtEquals(39991L, aa.getLong("key3", 0L));
Asrt.asrtEquals(true, bb.getBoolean("key4", fal));
demonstration
Asrt.asrtEquals(5.3f, cc.getFloat("key5", 0.f));
Asrt.asrtEquals(99, dd.getInt("key6", 1));
Asrt.asrtEquals("如果打印本信息, 证明参数不相等", 10L, 10);
按照源码分析, 我们可以把⼀个预期结果作为1参传递进去. 2参传递我们需要测试的⽅法. 然后执⾏. 相等, 代码继续往下执⾏,不相等, 中断执⾏, 抛出异常信息
略作⼀提:
Asrt.asrtSame(Object expected, Object actual)⽅法:
查看源码, 其⽐较的是引⽤地址是否相等, 并没有对内容进⾏⽐较:
/**
* Asrts that two objects refer to the same object. If they are not
* the same an AsrtionFailedError is thrown.
*/
static public void asrtSame(Object expected, Object actual) {
asrtSame(null, expected, actual);
benjaminfranklin}
/**
* Asrts that two objects refer to the same object. If they are not
* an AsrtionFailedError is thrown with the given message.
三叉戟怎么读*/
static public void asrtSame(String message, Object expected, Object actual) {
if (expected == actual) {
return;
}
failNotSame(message, expected, actual);
}
到此这篇关于Java Asrt.asrtEquals案例详解的⽂章就介绍到这了,更多相关Java Asrt.asrtEquals内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

本文发布于:2023-06-15 19:02:49,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/146179.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:相等   类型   信息   传递
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图