我想斷言List<Achievement>
包含TestAchievement
類型的成員。Java Hamcrest:Collection包含的項目類型
這裏是我的說法:
List<Achievement> achievements; // Populated elsewhere
assertThat(achievements,hasItem(isA(TestAchievement.class)));
這並不編譯,報告錯誤:
The method assertThat(T, Matcher) in the type Assert is not applicable for the arguments (List, Matcher<Iterable<TestAchievement>>)
什麼是使用Hamcrest這種說法的正確語法?
這是一個很好的例子。適用於我,但只適用於Hamcrest 1.1。升級到1.2或1.3.0RC0會給我n錯誤。 – 2011-03-17 10:55:38
的錯誤(1.2)堆棧跟蹤: java.lang.NoSuchMethodError:org.hamcrest.Matcher.describeMismatch(Ljava /郎/對象; Lorg/hamcrest /描述;)V \t在org.hamcrest.core.IsCollectionContaining。 matchesSafely(IsCollectionContaining.java:31) \t在org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14) \t在org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:54) \t在org.hamcrest .MatcherAssert.assertThat(MatcherAssert.java:12) \t at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) – 2011-03-17 10:56:41
@JesperRønn-Jensen請參閱[本答案] (http://stackoverflow.com/questions/4981586/java-hamcrest-collection-contains-item-of-type/20751480#20751480)。這與hamcrest 1.1和1.3一起工作。 – 2013-12-23 21:46:37