|
1 | | -import java.util.Iterator; |
2 | | -import java.util.List; |
| 1 | +package com.baeldung.generics; |
3 | 2 |
|
4 | 3 | import org.junit.Test; |
5 | 4 |
|
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import static org.hamcrest.CoreMatchers.hasItems; |
| 8 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 9 | + |
6 | 10 | public class GenericsTest { |
7 | 11 |
|
8 | | - // testing the generic method with Integer |
9 | | - @Test |
10 | | - public void fromArrayToListIntTest() { |
11 | | - Integer[] intArray = { 1, 2, 3, 4, 5 }; |
12 | | - 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 | | - } |
18 | | - } |
19 | | - |
20 | | - // testing the generic method with String |
21 | | - @Test |
22 | | - public void fromArrayToListStringTest() { |
23 | | - String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" }; |
24 | | - 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 | | - } |
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 fromArrayToListUpperboundIntTest() { |
37 | | - Integer[] intArray = { 1, 2, 3, 4, 5 }; |
38 | | - 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 | | - } |
44 | | - } |
| 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 | + } |
45 | 40 |
|
46 | 41 | } |
0 commit comments