JsonUnit

Let me introduce you another of my pet projects. It's called JsonUnit and it's something like XmlUnit only for JSON (well it's much, much more simple). Basically it's able to compare two JSON documents and if they do not match, it prints out the differences. For example the following test

import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals;

...

assertJsonEquals("{\n" + 
			"   \"test\":[\n" + 
			"      1,\n" + 
			"      2,\n" + 
			"      {\n" + 
			"         \"child\":{\n" + 
			"            \"value1\":1,\n" + 
			"            \"value2\":true,\n" + 
			"            \"value3\":\"test\",\n" + 
			"            \"value4\":{\n" + 
			"               \"leaf\":5\n" + 
			"            }\n" + 
			"         }\n" + 
			"      }\n" + 
			"   ],\n" + 
			"   \"root2\":false,\n" + 
			"   \"root3\":1\n" + 
			"}", 
			"{\n" + 
			"   \"test\":[\n" + 
			"      5,\n" + 
			"      false,\n" + 
			"      {\n" + 
			"         \"child\":{\n" + 
			"            \"value1\":5,\n" + 
			"            \"value2\":\"true\",\n" + 
			"            \"value3\":\"test\",\n" + 
			"            \"value4\":{\n" + 
			"               \"leaf2\":5\n" + 
			"            }\n" + 
			"         },\n" + 
			"         \"child2\":{\n" + 
			"\n" + 
			"         }\n" + 
			"      }\n" + 
			"   ],\n" + 
			"   \"root4\":\"bar\"\n" + 
			"}");

will result in

java.lang.AssertionError: JSON documents are different:
Different keys found in node "". Expected [root2, root3, test], got [root4, test].
Different value found in node "test[0]". Expected 1, got 5.
Different types found in node "test[1]". Expected NUMBER, got BOOLEAN.
Different keys found in node "test[2]". Expected [child], got [child, child2].
Different value found in node "test[2].child.value1". Expected 1, got 5.
Different types found in node "test[2].child.value2". Expected BOOLEAN, got STRING.
Different keys found in node "test[2].child.value4". Expected [leaf], got [leaf2].

Neat, isn't it?

Tags: , ,

One Response to “JsonUnit”

  1. Martiner Says:

    It definitely is!