1- import static org . hamcrest . CoreMatchers . hasItems ;
2- import static org . hamcrest . MatcherAssert . assertThat ;
3- import static org .junit .Assert . assertTrue ;
1+ package com . baeldung . generics ;
2+
3+ import org .junit .Test ;
44
55import java .util .ArrayList ;
66import java .util .List ;
77
8- import org .junit .Test ;
8+ import static org .hamcrest .CoreMatchers .hasItems ;
9+ import static org .hamcrest .MatcherAssert .assertThat ;
10+ import static org .junit .Assert .assertTrue ;
911
1012public class GenericsTest {
1113
12- // testing the generic method with Integer
13- @ Test
14- public void givenArrayOfIntegers_thanListOfIntegersReturnedOK () {
15- Integer [] intArray = { 1 , 2 , 3 , 4 , 5 };
16- List <Integer > list = Generics .fromArrayToList (intArray );
17-
18- assertThat (list , hasItems (intArray ));
19- }
20-
21- // testing the generic method with String
22- @ Test
23- public void givenArrayOfStrings_thanListOfStringsReturnedOK () {
24- String [] stringArray = { "hello1" , "hello2" , "hello3" , "hello4" , "hello5" };
25- List <String > list = Generics .fromArrayToList (stringArray );
26-
27- assertThat (list , hasItems (stringArray ));
28- }
29-
30- // testing the generic method with Number as upper bound with Integer
31- // if we test fromArrayToListWithUpperBound with any type that doesn't
32- // extend Number it will fail to compile
33- @ Test
34- public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK () {
35- Integer [] intArray = { 1 , 2 , 3 , 4 , 5 };
36- List <Integer > list = Generics .fromArrayToListWithUpperBound (intArray );
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-
53- }
14+ // testing the generic method with Integer
15+ @ Test
16+ public void givenArrayOfIntegers_thanListOfIntegersReturnedOK () {
17+ Integer [] intArray = {1 , 2 , 3 , 4 , 5 };
18+ List <Integer > list = Generics .fromArrayToList (intArray );
19+
20+ assertThat (list , hasItems (intArray ));
21+ }
22+
23+ // testing the generic method with String
24+ @ Test
25+ public void givenArrayOfStrings_thanListOfStringsReturnedOK () {
26+ String [] stringArray = {"hello1" , "hello2" , "hello3" , "hello4" , "hello5" };
27+ List <String > list = Generics .fromArrayToList (stringArray );
28+
29+ assertThat (list , hasItems (stringArray ));
30+ }
31+
32+ // testing the generic method with Number as upper bound with Integer
33+ // if we test fromArrayToListWithUpperBound with any type that doesn't
34+ // extend Number it will fail to compile
35+ @ Test
36+ public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK () {
37+ Integer [] intArray = {1 , 2 , 3 , 4 , 5 };
38+ List <Integer > list = Generics .fromArrayToListWithUpperBound (intArray );
39+
40+ assertThat (list , hasItems (intArray ));
41+ }
42+
43+ // testing paintAllBuildings method with a subtype of Building, the method
44+ // will work with all subtypes of Building
45+ @ Test
46+ public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK () {
47+
48+ List <Building > subBuildingsList = new ArrayList <>();
49+ subBuildingsList .add (new Building ());
50+ subBuildingsList .add (new House ());
51+
52+ boolean result = Generics .paintAllBuildings (subBuildingsList );
53+ assertTrue (result );
54+ }
5455
5556}
0 commit comments