44import org .adoptopenjdk .lambda .tutorial .exercise3 .Book ;
55import org .adoptopenjdk .lambda .tutorial .exercise3 .Books ;
66import org .adoptopenjdk .lambda .tutorial .exercise3 .Publisher ;
7+ import org .adoptopenjdk .lambda .tutorial .util .FeatureMatchers ;
8+ import org .hamcrest .Matcher ;
79import org .junit .Test ;
810
911import java .util .Arrays ;
1517import java .util .stream .Stream ;
1618
1719import static org .hamcrest .MatcherAssert .assertThat ;
18- import static org .hamcrest .Matchers .contains ;
20+ import static org .hamcrest .Matchers .containsInAnyOrder ;
21+ import static org .hamcrest .Matchers .equalTo ;
1922
2023/**
2124 * Exercise 3 - Mapping
8386 * @see Collectors#toList()
8487 *
8588 */
89+ @ SuppressWarnings ("unchecked" )
8690public class Exercise_3_Test {
8791
8892 private final Author joshuaBloch = new Author ("Joshua" , "Bloch" );
@@ -104,7 +108,7 @@ public class Exercise_3_Test {
104108 @ Test
105109 public void getAllBookTitles () {
106110 assertThat (Books .titlesOf (books ),
107- contains ("Effective Java" , "Java Concurrency In Practice" , "Java For Dummies" ));
111+ containsInAnyOrder ("Effective Java" , "Java Concurrency In Practice" , "Java For Dummies" ));
108112 }
109113
110114 /**
@@ -118,7 +122,7 @@ public void getAllBookTitles() {
118122 @ Test
119123 public void getNamesOfAuthorsOfBooks () {
120124 assertThat (Books .namesOfAuthorsOf (books ),
121- contains ("Joshua Bloch" , "Brian Goetz" , "Barry Burd" ));
125+ containsInAnyOrder ("Joshua Bloch" , "Brian Goetz" , "Barry Burd" ));
122126 }
123127
124128 /**
@@ -136,7 +140,14 @@ public void getNamesOfAuthorsOfBooks() {
136140 @ Test
137141 public void getPublishersRepresentedByBooks () {
138142 assertThat (Books .publishersRepresentedBy (books ),
139- contains (addisonWesley , johnWileyAndSons ));
143+ containsInAnyOrder (publisherNamed ("Addison-Wesley" ), publisherNamed ("John Wiley & Sons" )));
144+ }
145+
146+
147+ // Test helpers
148+
149+ private static Matcher <Publisher > publisherNamed (String name ) {
150+ return FeatureMatchers .from (equalTo (name ), "is named" , "name" , p -> p .name );
140151 }
141152
142153}
0 commit comments