5
votes

i've been trying assert that my Set has collection with given property with hamcrest, using this solution, but i have :

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

imports:

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

code:

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

have You any ideas how to assert it well ?

2

2 Answers

2
votes

Well, you should try to let assertThat do the work for you.

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

assertThat(mySet, is(expectedSet))

The restriction here: that assumes that your set contains only that one property value.

Otherwise, you can go for:

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

(probably with an additional message to better describe a failing assert).

Prereq: your property class should be implementing equals() in a reasonable manner.

0
votes

Another approach will be:

assertTrue(mySet.contains(someProperty);