Is there a Hamcrest "for each" Matcher that asserts all elements of a Collection or Iterable match a single specific Matcher?
Asked Answered
E

2

80

Given a Collection or Iterable of items, is there any Matcher (or combination of matchers) that will assert every item matches a single Matcher?

For example, given this item type:

public interface Person {
    public String getGender();
}

I'd like to write an assertion that all items in a collection of Persons have a specific gender value. I'm thinking something like this:

Iterable<Person> people = ...;
assertThat(people, each(hasProperty("gender", "Male")));

Is there any way to do this without writing the each matcher myself?

Escarpment answered 4/3, 2015 at 16:48 Comment(1)
Probably you can use the everyItem Matcher: junit.sourceforge.net/javadoc/org/junit/matchers/…Hadsall
A
108

Use the Every matcher.

import org.hamcrest.beans.HasPropertyWithValue;
import org.hamcrest.core.Every;
import org.hamcrest.core.Is;
import org.junit.Assert;

Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty("gender", Is.is("male")))));

Hamcrest also provides Matchers#everyItem as a shortcut to that Matcher.


Full example

@org.junit.Test
public void method() throws Exception {
    Iterable<Person> people = Arrays.asList(new Person(), new Person());
    Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty("gender", Is.is("male")))));
}

public static class Person {
    String gender = "male";

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}
Alanaalanah answered 4/3, 2015 at 16:59 Comment(5)
It seems that generics is causing problems. I'm getting a compilation problem because assertThat() expects argument types T, Matcher<?super T> but it's getting Iterable<Person>, Matcher<Iterable<Object>>Escarpment
@Escarpment I've updated with a full example. If yours is different, please edit your question to include it.Alanaalanah
You example still has the generics-related compilation problem, but only when compiled against JDK 7; with JDK 8 it compiles fine. I wasn't aware that the generics spec changed between 7 and 8 but apparently they realized the compiler was being stupid and fixed it.Escarpment
To get it to compile for Java 7, I had to declare the hasProperty Matcher like this: HasPropertyWithValue.<Person>hasProperty("gender", is("Male")) which pretty much destroys the readability improvements that Hamcrest is supposed to provide :-(Escarpment
And beyond that, woohaaa, reflection and by-string access. I find that super-ugly. I would rather manually iterate the collection and assertThat() on Person::getGender() than ...Jews
R
-2

More readable than the approved answer and no separate assertions in a loop:

import static org.assertj.core.api.Assertions.assertThat;

assertThat(people).allMatch((person) -> {
  return person.gender.equals("male");
});
Roderic answered 3/5, 2021 at 13:24 Comment(1)
but that is not HamcrestBlanchette

© 2022 - 2024 — McMap. All rights reserved.