0

AssertJ provides the method hasValueSatisfying(Consumer<T> requirement) for Java Optional objects. This method allows to create assertions on the optional value like this:

    assertThat(myOptional).hasValueSatisfying(v -> {
        assertThat(v.getFirstName()).isEqualTo("Stephen");
        assertThat(v.getLastName()).isEqualTo("Smith");
        assertThat(v.getAge()).isEqualTo(22);
    });

Is there any equivalent method for collections? Of cause I can misuse the method allMatch(Predicate<? super T> predicate) like this

    assertThat(myIcelanderFriends).extracting(Person::getAddress)
                    .allMatch(a -> {
                        assertThat(a.getCountry()).isEqualTo("Iceland");
                        assertThat(a.getPhoneContryCode()).isEqualTo("+354");
                        assertThat(a.getSurname()).endsWith("son");
                        return true; // Predictes must return a boolean
                    });

but then I have to add the misleading return true; line as a Predicate needs a return value. I'd prefer to have here also haveValuesSatisfying(Consumer<T> requirement) method. Did I oversee such a method or does it not exist (yet)?

Joern
  • 1,926
  • 1
  • 13
  • 18
  • `myIcelanderFriends.stream().map(Person::getAddress).forEach(a -> { assertThat(...); ...});` ? – JB Nizet Jul 14 '16 at 08:56
  • @JBNizet Of cause your suggestion is a valid solution. However, from my point of view it has the disadvantage that I cannot imidiately see that this line of code performs some assertions, as it does not start with `assertThat` – Joern Jul 14 '16 at 09:31

1 Answers1

1

Nope there is no such method yet ... but will be in 3.6.0 : https://github.com/joel-costigliola/assertj-core/issues/711

Joel Costigliola
  • 6,308
  • 27
  • 35