I use hamcrest for most of my testing ,but have encountered a issue with it not being able to test a property one level down in the object graph .A snipped of my test case is below
final List<Foo> foos= fooRepository.findAll(spec);
assertThat(results, is(notNullValue()));
assertThat(results, hasItem(hasProperty("id.fooID1", equalTo("FOOID1"))));
so here I want to check if in the list of foos I have a property id.fooID1 equla to FOOID1 .Here I am going one level down to check my nested property .This doesnt currently work in hamcrest and I get the following error.
java.lang.AssertionError:
Expected: a collection containing hasProperty("id.fooID1", "FOOID1")
but: No property "id.fooID1"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
any help or workaround on this issue .
You can nest hasProperty
calls:
assertThat(results, hasItem(hasProperty("id", hasProperty("fooID1", equalTo("FOOID1")))));
For deeper nestings this might be a bit unwieldy.