1- import java .util .Iterator ;
1+ import static org .hamcrest .CoreMatchers .hasItems ;
2+ import static org .hamcrest .MatcherAssert .assertThat ;
3+ import static org .junit .Assert .assertTrue ;
4+
5+ import java .util .ArrayList ;
26import java .util .List ;
37
48import org .junit .Test ;
@@ -7,40 +11,45 @@ public class GenericsTest {
711
812 // testing the generic method with Integer
913 @ Test
10- public void fromArrayToListIntTest () {
14+ public void givenArrayOfIntegers_thanListOfIntegersReturnedOK () {
1115 Integer [] intArray = { 1 , 2 , 3 , 4 , 5 };
1216 List <Integer > list = Generics .fromArrayToList (intArray );
13- Iterator <Integer > iterator ;
14- iterator = list .iterator ();
15- while (iterator .hasNext ()) {
16- System .out .println (iterator .next ());
17- }
17+
18+ assertThat (list , hasItems (intArray ));
1819 }
1920
2021 // testing the generic method with String
2122 @ Test
22- public void fromArrayToListStringTest () {
23+ public void givenArrayOfStrings_thanListOfStringsReturnedOK () {
2324 String [] stringArray = { "hello1" , "hello2" , "hello3" , "hello4" , "hello5" };
2425 List <String > list = Generics .fromArrayToList (stringArray );
25- Iterator <String > iterator ;
26- iterator = list .iterator ();
27- while (iterator .hasNext ()) {
28- System .out .println (iterator .next ());
29- }
26+
27+ assertThat (list , hasItems (stringArray ));
3028 }
3129
3230 // testing the generic method with Number as upper bound with Integer
3331 // if we test fromArrayToListWithUpperBound with any type that doesn't
3432 // extend Number it will fail to compile
3533 @ Test
36- public void fromArrayToListUpperboundIntTest () {
34+ public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK () {
3735 Integer [] intArray = { 1 , 2 , 3 , 4 , 5 };
3836 List <Integer > list = Generics .fromArrayToListWithUpperBound (intArray );
39- Iterator <Integer > iterator ;
40- iterator = list .iterator ();
41- while (iterator .hasNext ()) {
42- System .out .println (iterator .next ());
43- }
37+
38+ assertThat (list , hasItems (intArray ));
39+ }
40+
41+ // testing paintAllBuildings method with a subtype of Building, the method
42+ // will work with all subtypes of Building
43+ @ Test
44+ public void givenSubTypeOfAwildCardBoundedGenericMethod () {
45+
46+ List <SubBuilding > subBuildingsList = new ArrayList <>();
47+ subBuildingsList .add (new SubBuilding ());
48+ subBuildingsList .add (new SubBuilding ());
49+
50+ boolean result = Generics .paintAllBuildings (subBuildingsList );
51+ assertTrue (result );
52+
4453 }
4554
46- }
55+ }
0 commit comments