- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在使用 JUnit 和 Hamcrest Matchers 测试 Set
时我注意到 Matchers.contains()
方法提供了关于测试错误的很好的线索。另一方面,Matchers.containsInAnyOrder()
差异报告几乎没有用。这是测试代码:
简单 bean:
public class MyBean {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
JUnit 测试:
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class MyTest {
@Test
public void hamcrestTest() {
Set<MyBean> beanSet = new HashSet<MyBean>();
MyBean bean = new MyBean();
bean.setId(1);
beanSet.add(bean);
bean = new MyBean();
bean.setId(2);
beanSet.add(bean);
assertThat(beanSet, contains(
hasProperty("id", is(1)),
hasProperty("id", is(3))
));
}
}
如您所见,实际的 bean id 是 1
和 2
,而预期的是 1
和 3
所以测试失败。
测试结果:
java.lang.AssertionError:
Expected: iterable over [hasProperty("id", is <1>), hasProperty("id", is <3>)] in any order
but: Not matched: <MyBean@4888884e
如果我切换到 Matchers.contains()
方法,那么结果会提供更多信息:
java.lang.AssertionError:
Expected: iterable containing [hasProperty("id", is <1>), hasProperty("id", is <3>)]
but: item 0: property 'id' was <2>
不幸的是,由于 Set 未排序 contains()
在这种情况下不是一个选项。
最后是问题:
在使用 hamcrest 断言 Set
时,是否有可能以某种方式获得更好的错误报告?
最佳答案
对于如何为 contains
和 containsInAnyOrder
匹配器报告不匹配,Hamcrest 似乎有不同的实现。
containsInAnyOrder
只是通过执行此操作为您提供项目的 toString()
值:
mismatchDescription.appendText("Not matched: ").appendValue(item);
虽然 contains
匹配器通过委托(delegate)给实际匹配器的 describeMismatch()
来做得更好:
matcher.describeMismatch(item, mismatchDescription);
因此,在这种情况下,您会看到 hasProperty
匹配器的附加信息,但在使用 containsInAnyOrder
时却看不到。
我认为在这种情况下你能做的最好的事情就是为你的 MyBean
类实现一个 toString()
。
已经有关于此问题的报告:https://github.com/hamcrest/JavaHamcrest/issues/47
关于java - JUnit 和 hamcrest : could containsInAnyOrder() tell more about the mismatch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29652265/
有什么区别?据我了解,Tell (object, IActorRef) 发送原始发件人。但为什么不只使用 Forward 方法呢? 谢谢 最佳答案 您可以将actorRef.Tell(msg) 视为a
我是 akka 的初学者。有人可以向我解释为什么 tell 方法定义如下: receiver.tell(msg, sender); 在我看来,有人向其他人发送消息,在 OOP 中它会这样: sende
每次运行此脚本时,我都会收到此错误:系统事件出现错误:“Test123”不理解通知消息。 代码: --more code... tell application "System Events"
在 Core Python Programming 这本书中,有以下示例 - >>> f = open('/tmp/x', 'w+')>>> f.tell()0>>> f.write('test li
能否请您告诉我 off_t tell(int fd) 函数在 Mac OS X 中的何处定义?它不在 fcntl.h 中,也不在 unistd.h 中,它在 UNIX 中定义...我不能使用 >fte
Python tell()函数:获得当前读取位置 文件可以被看作是字节流或者字符流。第一个读入的字符是在该流的第 0 个位置上。如果读入了 5 个字符,则现在在该流的第 5 个位置上。 我们可以用 t
版本 3.1.3(如果相关)。 这个建议现在可能有效,但将来会有效吗? NSString *device = [UIDevice currentDevice].model; if([device is
docker异常停止时,第一时间找不到。 有没有办法在它停止时通过邮件或其他任何方式发送给我? 我用shipyard来管理,但是没有这个功能。 我能否找到一种方法来监控docker意外停止并向用户发送
我在使用 Java 套接字 API 时遇到了一些问题。我正在尝试显示当前连接到我的游戏的玩家数量。很容易确定玩家何时已连接。然而,使用套接字 API 来确定玩家何时断开连接似乎不必要地困难。 在已远程
我在使用 Java 套接字 API 时遇到了一些问题。我正在尝试显示当前连接到我的游戏的玩家数量。很容易确定玩家何时已连接。然而,使用套接字 API 来确定玩家何时断开连接似乎不必要地困难。 在已远程
我正在尝试复制 Compass 应用程序中的功能 - 但我卡在了一个特定的位置:我如何确定界面中的“向上”方向? 我在屏幕上有一个标签,并且我有以下代码可以在设备四处移动时使其保持水平: self.m
我开始学习Akka并从official guid下载了示例: 我不明白tell方法第二个参数的用法: 在main方法中写入: howdyGreeter.tell(new WhoToGreet("Akk
我想在我的应用程序中添加一个“告诉 friend ”选项,允许用户选择多个联系人向他们发送电子邮件。联系人需要过滤为仅拥有电子邮件地址的联系人。 有谁知道我可以重复使用的现成示例吗? 最佳答案 我最近
我不明白为什么 tell() 函数在这种情况下不起作用。让我们创建一个包含字符串“1\n2\n3\n4\n”的文件: f=open('test.tmp','w') f.write('1\n2\n3\n
我正在尝试打开一个文件并从上次读取的点开始读取。我的文件相当大(20 Mb 到 ~ 1 Gb) 经过一些研究后,似乎 tell() 和 seek() 将是执行此操作的最有效方法之一。我试过下面的代码
我在使用 Java 套接字 API 时遇到了一些问题。我正在尝试显示当前连接到我的游戏的玩家数量。很容易确定玩家何时已连接。然而,使用套接字 API 来确定玩家何时断开连接似乎不必要地困难。 在已远程
我在 Windows 上使用 Python 2.7,我是 Python 的新手,如果这很简单,请原谅我。 我读过的所有内容都说 tell() 返回“位置”,我相信这基本上是我们当前在阅读中所处的光标位
有没有人碰巧知道为什么要以这种方式遍历文件: 输入: f = open('test.txt', 'r') for line in f: print "f.tell(): ",f.tell()
试图在读取一行后使用tell() 获取光标位置。 text mode 和 \r 似乎有问题。我是在滥用该方法还是它是一个错误?谢谢。 用 python2.6、2.7、3.3 测试,输出相同。 impo
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭12 年前。 Improve th
我是一名优秀的程序员,十分优秀!