Posted In: Hamcrest, Junit, Testing

How to unit test a Map – JUnit + Hamcrest

Usecase – You need to unit test Map output and write multiple scenarios to test the data. Hamcrest library may be useful to achieve that

Hamcrest 1.3

<dependency>
	<groupId>org.hamcrest</groupId>
	<artifactId>hamcrest-library</artifactId>
	<version>1.3</version>
	<scope>test</scope>
</dependency>

Classes used from Hamcrest

org.hamcrest.collection.IsIn;
org.hamcrest.collection.IsMapContaining;

Using ErrorCollector so that execution continues even if assert statement fails.

package com.example.testing.hamcrest;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;

import java.util.HashMap;
import java.util.Map;

import org.hamcrest.collection.IsIn;
import org.hamcrest.collection.IsMapContaining;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class TestHamcrestCollection3 {

	@Rule
	public ErrorCollector collector = new ErrorCollector();

	@Test
	public void testMapEquality() {

		Map<String, String> actual = new HashMap<>();
		actual.put("j", "junit");
		actual.put("h", "hamcrest");
		actual.put("m", "mysql");
		actual.put("o", "oracle");

		Map<String, String> expected = new HashMap<>();
		expected.put("j", "junit");
		expected.put("h", "hamcrest");
		expected.put("m", "mysql");
		expected.put("o", "oracle");

		// positive - equality
		collector.checkThat(actual, is(expected));

		// positive - equality
		collector.checkThat(actual.entrySet(),
		        everyItem(IsIn.isIn(expected.entrySet())));

		// positive - equality
		collector.checkThat(actual.entrySet(), equalTo(actual.entrySet()));

		// positive - size
		collector.checkThat(actual.size(), is(4));

		// positive - entry
		collector.checkThat(actual, IsMapContaining.hasEntry("h", "hamcrest"));

		// positive - multiple entries
		collector.checkThat(actual,
		        allOf(IsMapContaining.hasEntry("h", "hamcrest"),
		                IsMapContaining.hasEntry("m", "mysql"),
		                IsMapContaining.hasEntry("o", "oracle")));

		// negative - entry incorrect value
		collector.checkThat(actual, IsMapContaining.hasEntry("m", "mongodb"));

		// negative - entry incorrect key
		collector.checkThat(actual, IsMapContaining.hasEntry("s", "mysql"));

		// positive - key
		collector.checkThat(actual, IsMapContaining.hasKey("j"));

		// positive - value
		collector.checkThat(actual, IsMapContaining.hasValue("oracle"));

	}
}

Output

java.lang.AssertionError: 
Expected: map containing ["m"->"mongodb"]
     but: map was [<h=hamcrest>, <j=junit>, <m=mysql>, <o=oracle>]
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)
	at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)

java.lang.AssertionError: 
Expected: map containing ["s"->"mysql"]
     but: map was [<h=hamcrest>, <j=junit>, <m=mysql>, <o=oracle>]
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)
	at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)

Tags:

by , on August 12th, 2017

  • Categories