Extract the result of given method invocation on the Iterable's elements under test into a new Iterable, this new
Iterable becoming the Iterable under test.
It allows you to test the method results of the Iterable's elements instead of testing the elements themselves. This
is especially useful for classes that do not conform to the Java Bean's getter specification (i.e. public String
toString() or public String status() instead of public String getStatus()).
Let's take a look at an example to make things clearer :
// Build a array of WesterosHouse, a WesterosHouse has a method: public String sayTheWords()
List<WesterosHouse> greatHouses = new ArrayList<WesterosHouse>();
greatHouses.add(new WesterosHouse("Stark", "Winter is Coming"));
greatHouses.add(new WesterosHouse("Lannister", "Hear Me Roar!"));
greatHouses.add(new WesterosHouse("Greyjoy", "We Do Not Sow"));
greatHouses.add(new WesterosHouse("Baratheon", "Our is the Fury"));
greatHouses.add(new WesterosHouse("Martell", "Unbowed, Unbent, Unbroken"));
greatHouses.add(new WesterosHouse("Tyrell", "Growing Strong"));
// let's verify the words of the great houses of Westeros:
assertThat(greatHouses).extractingResultOf("sayTheWords")
.contains("Winter is Coming", "We Do Not Sow", "Hear Me Roar")
.doesNotContain("Lannisters always pay their debts");
Following requirements have to be met to extract method results:
- method has to be public,
- method cannot accept any arguments,
- method cannot return void.
Note that the order of extracted results is consistent with the iteration order of the Iterable under test, for
example if it's a
HashSet, you won't be able to make any assumptions on the extracted results order.