diff --git a/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java b/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java index 5759cc76d6..6870cdef23 100644 --- a/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java +++ b/java-collections-maps-3/src/test/java/com/baeldung/map/invert/InvertHashMapUnitTest.java @@ -1,5 +1,6 @@ package com.baeldung.map.invert; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -8,7 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; @@ -17,44 +18,55 @@ public class InvertHashMapUnitTest { Map sourceMap; - @BeforeAll + @BeforeEach void setup() { sourceMap = new HashMap<>(); sourceMap.put("Sunday", 0); sourceMap.put("Monday", 1); + sourceMap.put("Tuesday", 2); + sourceMap.put("Wednesday", 3); + sourceMap.put("Thursday", 4); + sourceMap.put("Friday", 5); + sourceMap.put("Saturday", 6); } @Test - void test1_invertMapUsingForLoop() { + void givenSourceMap_whenUsingForLoop_returnsInvertedMap() { Map inversedMap = InvertHashMapExample.invertMapUsingForLoop(sourceMap); + assertNotNull(inversedMap); - assertEquals(inversedMap.size(), 2); + assertEquals(sourceMap.size(), inversedMap.size()); assertEquals("Monday", inversedMap.get(1)); } @Test - void test2_invertMapUsingStreams() { + void givenSourceMap_whenUsingStreams_returnsInvertedMap() { Map inversedMap = InvertHashMapExample.invertMapUsingStreams(sourceMap); + assertNotNull(inversedMap); - assertEquals(inversedMap.size(), 2); + assertEquals(sourceMap.size(), inversedMap.size()); assertEquals("Monday", inversedMap.get(1)); } @Test - void test3_invertMapUsingMapper() { + void givenSourceMap_whenUsingMapper_returnsInvertedMap() { Map inversedMap = InvertHashMapExample.invertMapUsingMapper(sourceMap); + assertNotNull(inversedMap); - assertEquals(inversedMap.size(), 2); + assertEquals(sourceMap.size(), inversedMap.size()); assertEquals("Monday", inversedMap.get(1)); } @Test - void test4_invertMapUsingGroupingBy() { - sourceMap.put("monday", 1); + void givenSourceMapWithDuplicateValues_whenUsingGroupBy_returnsInvertedMap() { + sourceMap.put("MONDAY", 1); Map> inversedMap = InvertHashMapExample.invertMapUsingGroupingBy(sourceMap); + assertNotNull(inversedMap); - assertEquals(inversedMap.size(), 2); - assertTrue(inversedMap.get(1) instanceof List); + assertNotEquals(sourceMap.size(), inversedMap.size()); // duplicate keys are merged now + assertEquals(2, inversedMap.get(1).size()); + assertTrue(inversedMap.get(1).contains("Monday")); + assertTrue(inversedMap.get(1).contains("MONDAY")); } }