Posted In: JSON

Check two JSON Objects for equality – Hamcrest-JSON

 

This example uses hamcrest-json library

 

<dependency>
	<groupId>uk.co.datumedge</groupId>
	<artifactId>hamcrest-json</artifactId>
	<version>0.2</version>
</dependency>

 

Examples using Hamcrest-JSON

 

1. Success test with a org.json.JSONObject

@Test
public void testJson1() {
	assertThat(expectedJsonObject1(),
	        SameJSONAs.sameJSONObjectAs(actualJsonObject1())
	                .allowingExtraUnexpectedFields()
	                .allowingAnyArrayOrdering());
}

private JSONObject expectedJsonObject1() {
	JSONObject expected = new JSONObject();
	expected.put("ID", "40");
	return expected;
}

private JSONObject actualJsonObject1() {
	JSONObject actual = new JSONObject();
	actual.put("ID", "40");
	return actual;
}

 

2. Error test with a org.json.JSONObject

@Test
public void testJson2() {
	assertThat(expectedJsonObject2(),
	        SameJSONAs.sameJSONObjectAs(actualJsonObject2())
	                .allowingExtraUnexpectedFields()
	                .allowingAnyArrayOrdering());
}

private JSONObject expectedJsonObject2() {
	JSONObject expected = new JSONObject();
	expected.put("ID", "40");
	return expected;
}

private JSONObject actualJsonObject2() {
	JSONObject actual = new JSONObject();
	actual.put("ID", "41");
	return actual;
}

 

3. Success test with a JSON String

@Test
public void testJson3() {
	assertThat(expectedData3(),
	        SameJSONAs.sameJSONAs(actualData3())
	                .allowingExtraUnexpectedFields()
	                .allowingAnyArrayOrdering());
}

private String expectedData3() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

private String actualData3() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

4. Error test with a JSON String

@Test
public void testJson4() {
	assertThat(expectedData4(),
	        SameJSONAs.sameJSONAs(actualData4())
	                .allowingExtraUnexpectedFields()
	                .allowingAnyArrayOrdering());
}

private String expectedData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

private String actualData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys1\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"200000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

Tags: , , ,

by , on February 24th, 2017

  • Categories