Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert that Set has item with exact property with hamcrest

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 ?

like image 660
Szympek Avatar asked Oct 30 '22 09:10

Szympek


2 Answers

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.

like image 192
GhostCat Avatar answered Nov 09 '22 09:11

GhostCat


Another approach will be:

assertTrue(mySet.contains(someProperty);

like image 34
Mamadou Ouologuem Avatar answered Nov 09 '22 09:11

Mamadou Ouologuem