Open GoogleCodeExporter opened 9 years ago
Original comment by t.denley
on 28 May 2013 at 10:53
Cristiano,
In my own search for a similar feature today I found I was able to satisfy my
requirement via a combination of the org.hamcrest.beans.HasPropertyWithValue
matcher and nested org.hamcrest.Matchers matchers.
For your example above, I came up with two possible assertThat's that may
satisfy your requirement, depending exactly what you need to check in the list
sub-property.
First idea:
Assert.assertThat(
"Expected myBean.map['myKey'].list to equal [\"one\", \"two\", \"three\"]",
myBean,
HasPropertyWithValue.<MyBean> hasProperty(
"map",
IsMapContaining.<String, MyOtherBean> hasEntry(
Matchers.equalTo("myKey"),
HasPropertyWithValue.<MyOtherBean> hasProperty("list",
Matchers.contains("one", "two", "three")))));
Second idea - perhaps closer to your original pseudocode's intent:
// assertThat(myBean, hasPropertyByExp("map['myKey'].list[3]", Matchers.equalTo("three")));
Assert.assertThat(
"Expected myBean.map['myKey'].list[3] to equal \"three\"",
myBean,
HasPropertyWithValue.<MyBean> hasProperty(
"map",
IsMapContaining.<String, MyOtherBean> hasEntry(
Matchers.equalTo("myKey"),
HasPropertyWithValue.<MyOtherBean> hasProperty(
"list",
Matchers.contains(Matchers.anything(), Matchers.anything(),
Matchers.equalTo("three"))))));
Does either of these do the job well enough for your need?
Hopefully putting the above in the public record helps us and other future
travellers of this Java testing path. :)
Mark
Original comment by mark.a.f...@gmail.com
on 27 Jan 2015 at 8:44
Original issue reported on code.google.com by
cristian...@gmail.com
on 28 May 2013 at 1:51