其他分享
首页 > 其他分享> > 如何断言Set具有与hamcrest具有精确属性的项目

如何断言Set具有与hamcrest具有精确属性的项目

作者:互联网

我一直在尝试使用这个solution断言我的Set有给定属性的集合,使用这个solution,但我有:

java.lang.NoSuchMethodError:
org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.Condition$Matched.matching(Condition.java:52)

进口:

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.junit.Assert.assertThat;

码:

assertThat(mySet, contains(hasProperty("id", equalTo("expectedId"))));

你有什么想法如何断言好吗?

解决方法:

好吧,你应该尝试让assertThat为你做的工作.

Set<WhateverPropertyTypeYouAreUsing> expectedSet = Collections.singleton( ... create a property object with that id/value);

assertThat(mySet, is(expectedSet))

这里的限制:假设您的集合仅包含该属性值.

否则,你可以去:

assertThat(mySet.contains(someProperty), is(true))

(可能还有一条额外的消息可以更好地描述失败的断言).

Prereq:您的属性类应该以合理的方式实现equals().

标签:java,assert,mocking,mockito,hamcrest
来源: https://codeday.me/bug/20190706/1396664.html