Posted In: JSON

Check two JSON Objects for equality – JSONAssert

 

This example uses JSONassert library

 

<dependency>
	<groupId>org.skyscreamer</groupId>
	<artifactId>jsonassert</artifactId>
	<version>1.4.0</version>
</dependency>

 

Examples using JSONassert

 

1. Success test with a org.json.JSONObject

	@Test
	public void testJson1() {
		JSONAssert.assertEquals(expectedJsonObject1(),
		        actualJsonObject1(), false);
	}

	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 testJson1() {
		JSONAssert.assertEquals(expectedJsonObject1(),
		        actualJsonObject1(), false);
	}

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

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

 

3. Success test with a JSON String

	@Test
	public void testJson3() {
		JSONAssert.assertEquals(expectedData3(),
		        actualData3(), false);
	}

	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() {
		JSONAssert.assertEquals(expectedData4(),
		        actualData4(), false);
	}

	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