How do I assert an Iterable contains elements with a certain property?
Asked Answered
L

10

177

Assume I want to unit test a method with this signature:

List<MyItem> getMyItems();

Assume MyItem is a Pojo that has many properties, one of which is "name", accessed via getName().

All I care about verifying is that the List<MyItem>, or any Iterable, contains two MyItem instances, whose "name" properties have the values "foo" and "bar". If any other properties don't match, I don't really care for the purposes of this test. If the names match, it's a successful test.

I would like it to be one-liner if possible. Here is some "pseudo-syntax" of the kind of thing I would like to do.

assert(listEntriesMatchInAnyOrder(myClass.getMyItems(), property("name"), new String[]{"foo", "bar"});

Would Hamcrest be good for this type of thing? If so, what exactly would be the hamcrest version of my pseudo-syntax above?

Longrange answered 28/8, 2012 at 19:43 Comment(0)
L
170

Thank you @Razvan who pointed me in the right direction. I was able to get it in one line and I successfully hunted down the imports for Hamcrest 1.3.

the imports:

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;

the code:

assertThat( myClass.getMyItems(), contains(
    hasProperty("name", is("foo")), 
    hasProperty("name", is("bar"))
));
Longrange answered 28/8, 2012 at 21:28 Comment(1)
If you don't know the order the items are use containsInAnyOrder (from same parent class) instead :)Hexavalent
T
126

AssertJ provides an excellent feature in extracting() : you can pass Functions to extract fields. It provides a check at compile time.
You could also assert the size first easily.

It would give :

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

assertThat(myClass.getMyItems())
          .hasSize(2)
          .extracting(MyItem::getName)
          .containsExactlyInAnyOrder("foo", "bar"); 

containsExactlyInAnyOrder() asserts that the list contains only these values whatever the order.

To assert that the list contains these values whatever the order but may also contain other values use contains() :

.contains("foo", "bar"); 

As a side note : to assert multiple fields from elements of a List , with AssertJ we do that by wrapping expected values for each element into a tuple() function :

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;

assertThat(myClass.getMyItems())
          .hasSize(2)
          .extracting(MyItem::getName, MyItem::getOtherValue)
          .containsExactlyInAnyOrder(
               tuple("foo", "OtherValueFoo"),
               tuple("bar", "OtherValueBar")
           ); 
Tyrolienne answered 12/8, 2018 at 19:33 Comment(4)
Don't get why this has no upvotes. I think, this is the best answer, by far.Pretermit
The assertJ library is much more readable then JUnit assertion API.Maxwellmaxy
@Maxwellmaxy Agreed and also I prefer it to hamcrest.Tyrolienne
In my opinion this is slightly less readable as it takes apart the "actual value" from the "expected value" and puts them in an order that needs to match.Wood
M
67

Its not especially Hamcrest, but I think it worth to mention here. What I use quite often in Java8 is something like:

assertTrue(myClass.getMyItems().stream().anyMatch(item -> "foo".equals(item.getName())));

(Edited to Rodrigo Manyari's slight improvement. It's a little less verbose. See comments.)

It may be a little bit harder to read, but I like the type and refactoring safety. Its also cool for testing multiple bean properties in combination. e.g. with a java-like && expression in the filter lambda.

Maples answered 19/10, 2015 at 9:50 Comment(6)
Slight improvement: assertTrue(myClass.getMyItems().stream().anyMatch(item -> "foo".equals(item.getName()));Lurlinelusa
@RodrigoManyari, closing parenthesis missingRapping
This solution waste the possibility to show an appropriate error message.Paramnesia
@GiulioCaccin I don't think it does. If you use JUnit, you could/should use the overloaded assertion methods and write assertTrue(..., "My own test failure message"); See more on junit.org/junit5/docs/current/api/org/junit/jupiter/api/…Maples
I mean, if you do the assertion against a Boolean, you lose the ability to print automatically the actual/expected difference. It is possible to assert using a matcher, but you need to modify this response to be similar to other in this page to do it.Paramnesia
Hm, I can't follow you. There is no difference or comparison here. The Question was, whether the collection contains items or not. So just do assertTrue(..., "The collection doesn't contain items with the name 'foo'") and in case of a failure, your test report will contain the correct message. If your message, for any reason, has to contain the test data, just add it to the massage.Maples
S
58

Try:

assertThat(myClass.getMyItems(),
                          hasItem(hasProperty("YourProperty", is("YourValue"))));
Silky answered 28/8, 2012 at 19:45 Comment(1)
just as a side node - this is a hamcrest solution (not assertj)Felic
U
31

Assertj is good at this.

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

    assertThat(myClass.getMyItems()).extracting("name").contains("foo", "bar");

Big plus for assertj compared to hamcrest is easy use of code completion.

Unwilled answered 22/12, 2015 at 13:39 Comment(1)
One more way Frank : assertThat(list) .containsAll(Arrays.asList(id1,id2));Kishke
M
10

As long as your List is a concrete class, you can simply call the contains() method as long as you have implemented your equals() method on MyItem.

// given 
// some input ... you to complete

// when
List<MyItems> results = service.getMyItems();

// then
assertTrue(results.contains(new MyItem("foo")));
assertTrue(results.contains(new MyItem("bar")));

Assumes you have implemented a constructor that accepts the values you want to assert on. I realise this isn't on a single line, but it's useful to know which value is missing rather than checking both at once.

Mosstrooper answered 28/8, 2012 at 19:51 Comment(5)
I really like your solution, but should he mod all that code for a test?Tomlinson
I figure that every answer here will require some test setup, execution of the method to test, and then assert the properties. There's no real overhead to my answer from what I can see, only that I have two assertions on seaprate lines so that a failed assertion can clearly identify what value is missing.Mosstrooper
It would be best to also include a message within assertTrue so that the error message is more intelligible. Without a message, if it fails, JUnit will just throw a AssertionFailedError without any error message. So best to include something like "results should contain new MyItem(\"foo\")".Arnaldo
Yes you are right. I'd recommend Hamcrest in any case, and I never use assertTrue() these daysMosstrooper
To a side note your POJO or DTO should define the equals methodImpenetrability
M
5

AssertJ 3.9.1 supports direct predicate usage in anyMatch method.

assertThat(collection).anyMatch(element -> element.someProperty.satisfiesSomeCondition())

This is generally suitable use case for arbitrarily complex condition.

For simple conditions I prefer using extracting method (see above) because resulting iterable-under-test might support value verification with better readability. Example: it can provide specialized API such as contains method in Frank Neblung's answer. Or you can call anyMatch on it later anyway and use method reference such as "searchedvalue"::equals. Also multiple extractors can be put into extracting method, result subsequently verified using tuple().

Maudiemaudlin answered 8/11, 2019 at 12:29 Comment(0)
W
2

Solution using org.assertj.core.api.AbstractAssert#asList()

Sometimes we need to assert an object that contains a property of the List type. I mean cases when it's not concise to assert the property itself explicitly in assertThat or any other similar methods (e.g. in case of assertThatExceptionOfType).

Here is the solution:

assertThat(myClass)
    .extracting(MyClass::getMyItems)
    .asList()
    .map(MyItem.class::cast)
    .extracting(MyItem::getName)
    .containsExactlyInAnyOrder("foo", "bar");

Pay attention on the map() method. Unfortunately, we can't simply use the asList() method, because after calling asList(), you get a list of Object elements, so you can't immediately extract properties of your type.

// isn't compiled
assertThat(myClass)
    .extracting(MyClass::getMyItems)
    .asList()
    .extracting(MyItem::getName)
    .containsExactlyInAnyOrder("foo", "bar");
Waxplant answered 31/1 at 7:49 Comment(0)
U
1

With Stream you can also do:

List<String> actual = myList.stream().map(MyClass::getName).collect(toList());
assertThat(actual, hasItem("expectedString1"));

Because with anyMatch() or allMatch(), you know some values in your list are in the list, but there is possibility that your actual list only contains 5 values while in anyMatch() you have 6; you don't know if all values are present or not. With hasItem(), you indeed check every value you want.

Ulani answered 15/9, 2021 at 13:6 Comment(0)
H
0

Alternatively to hasProperty you can try hamcrest-more-matchers where matcher with extracting function. In your case it will look like:

import static com.github.seregamorph.hamcrest.MoreMatchers.where;

assertThat(myClass.getMyItems(), contains(
    where(MyItem::getName, is("foo")), 
    where(MyItem::getName, is("bar"))
));

The advantages of this approach are:

  • It is not always possible to verify by field if the value is computed in get-method
  • In case of mismatch there should be a failure message with diagnostics (pay attention to resolved method reference MyItem.getName:
Expected: iterable containing [Object that matches is "foo" after call
MyItem.getName, Object that matches is "bar" after call MyItem.getName]
     but: item 0: was "wrong-name"
  • It works in Java 8, Java 11 and Java 14
Hassanhassell answered 9/10, 2020 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.