How to test POST JSON Object request using Java Map in Rest Assured

HOME

In the last tutorial, I have explained How to test POST request from JSON Object in Rest Assured where request body is built in JSONObject. In this tutorial, I will create a Request body using JSON Object in Rest Assured. 

We can create a JSON Object using a Map in Java. A JSON Object is a key-value pair and can be easily created using a Java Map. A Map in Java also represents a collection of key-value pairs.

I have created a simple Java map and filled it with the values that represent JSON properties.

	@Test
	public void passBodyAsMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("employee_name", "MapTest");
		map.put("employee_salary", "99999");
		map.put("employee_age", "30");
		map.put("profile_image", "test.png");
		RestAssured.given()
                          .contentType(ContentType.JSON)
                          .body(map)
                          .log().all()
				
				   .when()
                         .post("http://dummy.restapiexample.com/api/v1/create")
				
				   .then()
                         .assertThat().statusCode(200)
                         .body("data.employee_name", equalTo("MapTest"))
				         .body("data.employee_age", equalTo("30"))
                         .body("data.employee_salary", equalTo("99999"))
				         .body("message", equalTo("Successfully! Record has been added.")).log().all();
	}

The Request body as well as response body will look as shown below:-

Above one is a simple JSON Request Body. Lets take an example of Complex Request Body or nested Request Body as shown below.

@Test
	public void passBodyAsMultipleMap() {
		// First JSON Object using Hash Map
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("employee_name", "MapTest");
		data.put("profile_image", "test.png");
		// Second JSON Object using Hash Map
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("updated_message", "Details of New Resource");
		msg.put("employee_age", "30");
		data.put("details", msg);
		data.put("employee_salary", "99999");
		RestAssured.given().contentType(ContentType.JSON).body(data).log().all()
				// WHEN
				.when().post("http://dummy.restapiexample.com/api/v1/create")
				// THEN
				.then().assertThat().statusCode(200).body("data.employee_name", equalTo("MapTest"))
				.body("data.details.updated_message", equalTo("Details of New Resource"))
				.body("data.details.employee_age", equalTo("30")).body("data.employee_salary", equalTo("99999"))
				.body("message", equalTo("Successfully! Record has been added.")).log().all();
	}
}

The Request body as well as response body will look as shown below image. First part is the body of request and second part is the response provided by API.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s