2016-02-12 54 views
4

我一直在試圖斷言,我的設置有收集與hamcrest給定的屬性,使用此solution確切性質,但我有:如何斷言,設置有項目與hamcrest

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")))); 

你有什麼想法如何斷言它呢?

回答

1

那麼,你應該嘗試讓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()。

+0

如果我只是在測試中啓動同一個對象並將其置於我的返回集合中,我就會無法工作,因爲如果您知道我的意思,但我相信它會比較它是否相同的對象,但是,作品 'assertThat(mySet.contains(expectedValue),equalTo(true));' 謝謝:) – Szympek