11package org .baeldung .java .collections ;
22
33import java .util .ArrayList ;
4+ import java .util .Collections ;
45
56import org .junit .Assert ;
7+ import org .junit .Before ;
68import org .junit .Test ;
79
810public class CollectionsJoinAndSplitJUnitTest {
911
10- public ArrayList <String > sauces = new ArrayList <String >();
11- public ArrayList <String > cheeses = new ArrayList <String >();
12- public ArrayList <String > vegetables = new ArrayList <String >();
13-
14- public ArrayList <ArrayList <String >> ingredients = new ArrayList <ArrayList <String >>();
12+ private ArrayList <String > sauces = new ArrayList <>();
13+ private ArrayList <String > cheeses = new ArrayList <>();
14+ private ArrayList <String > vegetables = new ArrayList <>();
1515
16- public CollectionsJoinAndSplitJUnitTest () throws Exception {
17- whenGeneratingTestData_ShouldSucceed ();
18- }
16+ private ArrayList <ArrayList <String >> ingredients = new ArrayList <>();
1917
20- @ Test
21- public void whenGeneratingTestData_ShouldSucceed () throws Exception {
22- sauces .clear ();
18+ @ Before
19+ public void init () {
2320 sauces .add ("Olive Oil" );
2421 sauces .add ("Marinara" );
25-
26- cheeses .clear ();
27- cheeses .add ("Mozarella" );
22+
23+ cheeses .add ("Mozzarella" );
2824 cheeses .add ("Feta" );
2925 cheeses .add ("Parmesan" );
30-
31- vegetables .clear ();
26+
3227 vegetables .add ("Olives" );
3328 vegetables .add ("Spinach" );
3429 vegetables .add ("Green Peppers" );
35-
36- ingredients .clear ();
30+
3731 ingredients .add (sauces );
3832 ingredients .add (cheeses );
3933 ingredients .add (vegetables );
40-
41- Assert .assertTrue (sauces .size () == 2 );
42- Assert .assertTrue (cheeses .size () == 3 );
43- Assert .assertTrue (vegetables .size () == 3 );
44- Assert .assertTrue (ingredients .size () == 3 );
4534 }
46-
35+
4736 @ Test
48- public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed () throws Exception {
49- ArrayList <ArrayList <String >> toppings = new ArrayList <ArrayList < String > >();
50-
37+ public void givenThreeArrayLists_whenJoiningIntoOneArrayList_shouldSucceed () {
38+ ArrayList <ArrayList <String >> toppings = new ArrayList <>();
39+
5140 toppings .add (sauces );
5241 toppings .add (cheeses );
5342 toppings .add (vegetables );
@@ -57,18 +46,17 @@ public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() thr
5746 Assert .assertTrue (toppings .contains (cheeses ));
5847 Assert .assertTrue (toppings .contains (vegetables ));
5948 }
60-
61- @ Test
62- public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed () throws Exception {
63- ArrayList <ArrayList <String >> toppings = ingredients ;
64- ArrayList <ArrayList <String >> removedToppings = new ArrayList <ArrayList <String >>();
65-
66- removedToppings .add (toppings .remove (toppings .indexOf (vegetables )));
67-
68- Assert .assertTrue (removedToppings .contains (vegetables ));
69- Assert .assertTrue (removedToppings .size () == 1 );
70- Assert .assertTrue (toppings .size () == 2 );
71- Assert .assertTrue (toppings .contains (sauces ));
72- Assert .assertTrue (toppings .contains (cheeses ));
73- }
49+
50+ @ Test
51+ public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed () {
52+
53+ ArrayList <ArrayList <String >> removedToppings = new ArrayList <>();
54+ removedToppings .add (ingredients .remove (ingredients .indexOf (vegetables )));
55+
56+ Assert .assertTrue (removedToppings .contains (vegetables ));
57+ Assert .assertTrue (removedToppings .size () == 1 );
58+ Assert .assertTrue (ingredients .size () == 2 );
59+ Assert .assertTrue (ingredients .contains (sauces ));
60+ Assert .assertTrue (ingredients .contains (cheeses ));
61+ }
7462}
0 commit comments