6

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 ?

Community
  • 1
  • 1
Szympek
  • 183
  • 1
  • 2
  • 7

3 Answers3

3

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • i tought it won't work if I just initiate te same object in test and assert it's in my returned collection, because I tought it would compare if its the same object, if You know what I mean , but well, it works `assertThat(mySet.contains(expectedValue), equalTo(true));` thank You :) – Szympek Feb 12 '16 at 13:04
1

Another approach will be:

assertTrue(mySet.contains(someProperty);

0

Your original code is fine. Your problem is probably due to some version conflict. By the way, you should be using Hamcrest's assertThat (import static org.hamcrest.MatcherAssert.assertThat), instead of JUnit's.

andresp
  • 1,624
  • 19
  • 31