HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Standard Test methods for checking values in Lists, Maps etc

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mapscheckingstandardmethodsfortestlistsvaluesetc

Problem

I needed to check the values in various Lists and Maps. In my case, the various types of Objects that needed checking were of four types:

  • String



  • List



  • Map



  • Map>



So, I came up with the following types test methods:

```
@Test
public void testString() {
Assert.assertEquals("String Mismatch;", providedString, receivedString);
}

@Test
public void testList() {
Assert.assertNotNull("Provided List is null;", providedList);
Assert.assertNotNull("Received List is null;", receivedList);
Assert.assertEquals("Size mismatch for lists;", providedList.size(), receivedList.size());
Assert.assertTrue("Missing values in received list; Missing: " + CollectionUtils.subtract(providedList, receivedList) + "; ", receivedList.containsAll(providedList));
}

@Test
public void testMap() {
Assert.assertNotNull("Provided Map is null;", providedMap);
Assert.assertNotNull("Received Map is null;", receivedMap);
Assert.assertEquals("Size mismatch for maps;", providedMap.size(), receivedMap.size());
Assert.assertTrue("Missing keys in received map;", receivedMap.keySet().containsAll(providedMap.keySet()));

providedMap.keySet().stream().forEach((key) -> {
Assert.assertEquals("Value mismatch for key '" + key + "';", providedMap.get(key), receivedMap.get(key));
});
}

@Test
public void testMapList() {
Assert.assertNotNull("Provided Map is null;", providedMapList);
Assert.assertNotNull("Received Map is null;", receivedMapList);
Assert.assertEquals("Size mismatch for maps;", providedMapList.size(), receivedMapList.size());
Assert.assertTrue("Missing keys in received map;", receivedMapList.keySet().containsAll(providedMapList.keySet()));

providedMapList.keySet().stream().forEach((key) -> {
List pList = providedMapList.get(key);
List rList = receivedMapList.get(key);

Assert.assertNotNull("Provided List is null for key '" + key + "';", pList);
Assert.assertNotNull("Received List is n

Solution

Map equals has same coverage and it is not attached to implementation: See map.equals javadoc.

So "better" (at least, more compact code) would be:

assertEquals("something", providedMap, receivedMap);


Well, same for list. That means, you can use the same command as above.

assertEquals("something", providedList, receivedList);


And even more...

assertEquals("something", providedMapList, receivedMapList);


Sure, you will not see which key is missing.

So, you can create new static matchers, as assertEquals that could provide more information. But, assertEquals already prints the content of each container (executing toString of expected and received object), so you can figure out what is missing anyway.

Example:

java.lang.AssertionError: 
Expected :[1, 2, 3, 4, 6]
Actual   :[1, 2, 3, 4, 5]


Edited

If the output is too big, you can use your method. There are some options to follow:

-
Create a class with static methods (similar with org.junit.Assert)

public class ContainerAssert {

public static void assertEquals(Map providedMap, Map receivedMap) {
    Assert.assertNotNull("Provided Map is null;", providedMap);
    Assert.assertNotNull("Received Map is null;", receivedMap);
    Assert.assertEquals("Size mismatch for maps;", providedMap.size(), receivedMap.size());
    Assert.assertTrue("Missing keys in received map;", receivedMap.keySet().containsAll(providedMap.keySet()));

    providedMap.keySet().stream().forEach((key) -> {
        Assert.assertEquals("Value mismatch for key '" + key + "';", providedMap.get(key), receivedMap.get(key));
    });
  }
  // ...
}


-
Use Hamcrest

// with strict order
 List collection = Lists.newArrayList("ab", "cd", "ef");
 assertThat(collection, contains("ab", "cd", "ef"));


-
Use Hamcrest and create a specialized matcher

Code Snippets

assertEquals("something", providedMap, receivedMap);
assertEquals("something", providedList, receivedList);
assertEquals("something", providedMapList, receivedMapList);
java.lang.AssertionError: 
Expected :[1, 2, 3, 4, 6]
Actual   :[1, 2, 3, 4, 5]
public class ContainerAssert {

public static <U, V >void assertEquals(Map<U, V> providedMap, Map<U, V> receivedMap) {
    Assert.assertNotNull("Provided Map is null;", providedMap);
    Assert.assertNotNull("Received Map is null;", receivedMap);
    Assert.assertEquals("Size mismatch for maps;", providedMap.size(), receivedMap.size());
    Assert.assertTrue("Missing keys in received map;", receivedMap.keySet().containsAll(providedMap.keySet()));

    providedMap.keySet().stream().forEach((key) -> {
        Assert.assertEquals("Value mismatch for key '" + key + "';", providedMap.get(key), receivedMap.get(key));
    });
  }
  // ...
}

Context

StackExchange Code Review Q#133150, answer score: 4

Revisions (0)

No revisions yet.