88import java .util .Arrays ;
99import java .util .List ;
1010import java .util .Optional ;
11+ import java .util .stream .Collectors ;
1112import java .util .stream .Stream ;
1213
13- import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
14+ import static org .assertj .core .api .Assertions .assertThat ;
1415import static org .assertj .core .api .AssertionsForClassTypes .assertThatThrownBy ;
1516
1617public class StreamFilterUnitTest {
1718
1819 @ Test
19- public void givenListOfCustomers_whenFilterByLambda_thenGetTwo () {
20- List <Customer > customers = Arrays .asList (new Customer ("John P." , 15 ), new Customer ("Sarah M." , 200 ), new Customer ("Charles B." , 150 ), new Customer ("Mary T." , 1 ));
21-
22- long customersWithMoreThan100Points = customers
20+ public void givenListOfCustomers_whenFilterByPoints_thenGetTwo () {
21+ Customer john = new Customer ("John P." , 15 );
22+ Customer sarah = new Customer ("Sarah M." , 200 );
23+ Customer charles = new Customer ("Charles B." , 150 );
24+ Customer mary = new Customer ("Mary T." , 1 );
25+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
26+
27+ List <Customer > customersWithMoreThan100Points = customers
2328 .stream ()
2429 .filter (c -> c .getPoints () > 100 )
25- .count ( );
30+ .collect ( Collectors . toList () );
2631
27- assertThat (customersWithMoreThan100Points ).isEqualTo (2 );
32+ assertThat (customersWithMoreThan100Points ).hasSize (2 );
33+ assertThat (customersWithMoreThan100Points ).contains (sarah , charles );
34+ }
35+
36+ @ Test
37+ public void givenListOfCustomers_whenFilterByPointsAndName_thenGetOne () {
38+ Customer john = new Customer ("John P." , 15 );
39+ Customer sarah = new Customer ("Sarah M." , 200 );
40+ Customer charles = new Customer ("Charles B." , 150 );
41+ Customer mary = new Customer ("Mary T." , 1 );
42+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
43+
44+ List <Customer > charlesWithMoreThan100Points = customers
45+ .stream ()
46+ .filter (c -> c .getPoints () > 100 && c
47+ .getName ()
48+ .startsWith ("Charles" ))
49+ .collect (Collectors .toList ());
50+
51+ assertThat (charlesWithMoreThan100Points ).hasSize (1 );
52+ assertThat (charlesWithMoreThan100Points ).contains (charles );
2853 }
2954
3055 @ Test
3156 public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo () {
32- List <Customer > customers = Arrays .asList (new Customer ("John P." , 15 ), new Customer ("Sarah M." , 200 ), new Customer ("Charles B." , 150 ), new Customer ("Mary T." , 1 ));
57+ Customer john = new Customer ("John P." , 15 );
58+ Customer sarah = new Customer ("Sarah M." , 200 );
59+ Customer charles = new Customer ("Charles B." , 150 );
60+ Customer mary = new Customer ("Mary T." , 1 );
61+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
3362
34- long customersWithMoreThan100Points = customers
63+ List < Customer > customersWithMoreThan100Points = customers
3564 .stream ()
3665 .filter (Customer ::hasOverThousandPoints )
37- .count ( );
66+ .collect ( Collectors . toList () );
3867
39- assertThat (customersWithMoreThan100Points ).isEqualTo (2 );
68+ assertThat (customersWithMoreThan100Points ).hasSize (2 );
69+ assertThat (customersWithMoreThan100Points ).contains (sarah , charles );
4070 }
4171
4272 @ Test
4373 public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo () {
44- List <Optional <Customer >> customers = Arrays .asList (Optional .of (new Customer ("John P." , 15 )), Optional .of (new Customer ("Sarah M." , 200 )), Optional .empty (), Optional .of (new Customer ("Mary T." , 300 )), Optional .empty ());
74+ Optional <Customer > john = Optional .of (new Customer ("John P." , 15 ));
75+ Optional <Customer > sarah = Optional .of (new Customer ("Sarah M." , 200 ));
76+ Optional <Customer > mary = Optional .of (new Customer ("Mary T." , 300 ));
77+ List <Optional <Customer >> customers = Arrays .asList (john , sarah , Optional .empty (), mary , Optional .empty ());
4578
46- long customersWithMoreThan100Points = customers
79+ List < Customer > customersWithMoreThan100Points = customers
4780 .stream ()
4881 .flatMap (c -> c
4982 .map (Stream ::of )
5083 .orElseGet (Stream ::empty ))
5184 .filter (Customer ::hasOverThousandPoints )
52- .count ( );
85+ .collect ( Collectors . toList () );
5386
54- assertThat (customersWithMoreThan100Points ).isEqualTo (2 );
87+ assertThat (customersWithMoreThan100Points ).hasSize (2 );
88+ assertThat (customersWithMoreThan100Points ).contains (sarah .get (), mary .get ());
5589 }
5690
5791 @ Test
5892 public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException () {
59- List <Customer > customers = Arrays .asList (new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" ), new Customer ("Sarah M." , 200 ), new Customer ("Charles B." , 150 ),
60- new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" ));
93+ Customer john = new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" );
94+ Customer sarah = new Customer ("Sarah M." , 200 );
95+ Customer charles = new Customer ("Charles B." , 150 );
96+ Customer mary = new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" );
97+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
6198
6299 assertThatThrownBy (() -> customers
63100 .stream ()
@@ -67,21 +104,27 @@ public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException
67104
68105 @ Test
69106 public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException () {
70- List <Customer > customers = Arrays .asList (new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" ), new Customer ("Sarah M." , 200 ), new Customer ("Charles B." , 150 ),
71- new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" ));
107+ Customer john = new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" );
108+ Customer sarah = new Customer ("Sarah M." , 200 );
109+ Customer charles = new Customer ("Charles B." , 150 );
110+ Customer mary = new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" );
111+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
72112
73113 assertThatThrownBy (() -> customers
74114 .stream ()
75115 .filter ((ThrowingPredicate .unchecked (Customer ::hasValidProfilePhoto )))
76- .count ( )).isInstanceOf (WrappedException .class );
116+ .collect ( Collectors . toList () )).isInstanceOf (WrappedException .class );
77117 }
78118
79119 @ Test
80120 public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo () {
81- List <Customer > customers = Arrays .asList (new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" ), new Customer ("Sarah M." , 200 ), new Customer ("Charles B." , 150 ),
82- new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" ));
121+ Customer john = new Customer ("John P." , 15 , "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e" );
122+ Customer sarah = new Customer ("Sarah M." , 200 );
123+ Customer charles = new Customer ("Charles B." , 150 );
124+ Customer mary = new Customer ("Mary T." , 1 , "https://images.unsplash.com/photo-1543297057-25167dfc180e" );
125+ List <Customer > customers = Arrays .asList (john , sarah , charles , mary );
83126
84- long customersWithValidProfilePhoto = customers
127+ List < Customer > customersWithValidProfilePhoto = customers
85128 .stream ()
86129 .filter (c -> {
87130 try {
@@ -91,9 +134,10 @@ public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
91134 }
92135 return false ;
93136 })
94- .count ( );
137+ .collect ( Collectors . toList () );
95138
96- assertThat (customersWithValidProfilePhoto ).isEqualTo (2 );
139+ assertThat (customersWithValidProfilePhoto ).hasSize (2 );
140+ assertThat (customersWithValidProfilePhoto ).contains (john , mary );
97141 }
98142
99143 @ Test
@@ -110,6 +154,6 @@ public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowExcep
110154 throw new RuntimeException (e );
111155 }
112156 })
113- .count ( )).isInstanceOf (RuntimeException .class );
157+ .collect ( Collectors . toList () )).isInstanceOf (RuntimeException .class );
114158 }
115159}
0 commit comments