|
| 1 | +package com.baeldung.java.list; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class ListUnitTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void givenAFruitList_whenAddNewFruit_thenFruitIsAdded(){ |
| 18 | + List<String> fruits = new ArrayList<>(); |
| 19 | + assertEquals("Unexpected number of fruits in the list, should have been 0", 0, fruits.size()); |
| 20 | + |
| 21 | + fruits.add("Apple"); |
| 22 | + assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size()); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenAFruitList_whenContainsFruit_thenFruitIsInTheList(){ |
| 27 | + List<String> fruits = new ArrayList<>(); |
| 28 | + |
| 29 | + fruits.add("Apple"); |
| 30 | + assertTrue("Apple should be in the fruit list", fruits.contains("Apple")); |
| 31 | + assertFalse("Banana should not be in the fruit list", fruits.contains("Banana")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenAnEmptyFruitList_whenEmptyCheck_thenListIsEmpty(){ |
| 36 | + List<String> fruits = new ArrayList<>(); |
| 37 | + assertTrue("Fruit list should be empty", fruits.isEmpty()); |
| 38 | + |
| 39 | + fruits.add("Apple"); |
| 40 | + assertFalse("Fruit list should not be empty", fruits.isEmpty()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenAFruitList_whenIterateOverIt_thenFruitsAreInOrder(){ |
| 45 | + List<String> fruits = new ArrayList<>(); |
| 46 | + |
| 47 | + fruits.add("Apple"); // fruit at index 0 |
| 48 | + fruits.add("Orange");// fruit at index 1 |
| 49 | + fruits.add("Banana");// fruit at index 2 |
| 50 | + int index = 0; |
| 51 | + for (Iterator<String> it = fruits.listIterator(); it.hasNext(); ) { |
| 52 | + String fruit = it.next(); |
| 53 | + assertEquals("Fruits should be in order", fruits.get(index++), fruit); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenAFruitList_whenRemoveFruit_thenFruitIsRemoved(){ |
| 59 | + List<String> fruits = new ArrayList<>(); |
| 60 | + |
| 61 | + fruits.add("Apple"); |
| 62 | + fruits.add("Orange"); |
| 63 | + assertEquals("Unexpected number of fruits in the list, should have been 2", 2, fruits.size()); |
| 64 | + |
| 65 | + fruits.remove("Apple"); |
| 66 | + assertEquals("Unexpected number of fruits in the list, should have been 1", 1, fruits.size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenAFruitList_whenSetFruit_thenFruitIsUpdated(){ |
| 71 | + List<String> fruits = new ArrayList<>(); |
| 72 | + |
| 73 | + fruits.add("Apple"); |
| 74 | + fruits.add("Orange"); |
| 75 | + |
| 76 | + fruits.set(0, "Banana"); |
| 77 | + assertEquals("Fruit at index 0 should be Banana", "Banana", fruits.get(0)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void givenAFruitList_whenSort_thenFruitsAreSorted(){ |
| 82 | + List<String> fruits = new ArrayList<>(); |
| 83 | + |
| 84 | + fruits.add("Apple"); |
| 85 | + fruits.add("Orange"); |
| 86 | + fruits.add("Banana"); |
| 87 | + |
| 88 | + fruits.sort(Comparator.naturalOrder()); |
| 89 | + |
| 90 | + assertEquals("Fruit at index 0 should be Apple", "Apple", fruits.get(0)); |
| 91 | + assertEquals("Fruit at index 1 should be Banana", "Banana", fruits.get(1)); |
| 92 | + assertEquals("Fruit at index 2 should be Orange", "Orange", fruits.get(2)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void givenAFruitList_whenSublist_thenWeGetASublist(){ |
| 97 | + List<String> fruits = new ArrayList<>(); |
| 98 | + |
| 99 | + fruits.add("Apple"); |
| 100 | + fruits.add("Orange"); |
| 101 | + fruits.add("Banana"); |
| 102 | + |
| 103 | + List<String> fruitsSublist = fruits.subList(0, 2); |
| 104 | + assertEquals("Unexpected number of fruits in the sublist, should have been 2", 2, fruitsSublist.size()); |
| 105 | + |
| 106 | + assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsSublist.get(0)); |
| 107 | + assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsSublist.get(1)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void givenAFruitList_whenToArray_thenWeGetAnArray(){ |
| 112 | + List<String> fruits = new ArrayList<>(); |
| 113 | + |
| 114 | + fruits.add("Apple"); |
| 115 | + fruits.add("Orange"); |
| 116 | + fruits.add("Banana"); |
| 117 | + |
| 118 | + String[] fruitsArray = fruits.toArray(new String[0]); |
| 119 | + assertEquals("Unexpected number of fruits in the array, should have been 3", 3, fruitsArray.length); |
| 120 | + |
| 121 | + assertEquals("Fruit at index 0 should be Apple", "Apple", fruitsArray[0]); |
| 122 | + assertEquals("Fruit at index 1 should be Orange", "Orange", fruitsArray[1]); |
| 123 | + assertEquals("Fruit at index 2 should be Banana", "Banana", fruitsArray[2]); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments