(De)serializing POJOs in REST Assured

In this post, I’d like to demonstrate how you can leverage the ability of REST Assured to (de-)serialize Plain Old Java Objects (better known as POJOs) for more powerful testing.

As an example, we’ll use the following POJO representing a car and some of its properties:

public class Car {
	
	String make;
	String model;
	int year;
	
	public Car() {		
	}
	
	public Car(String make, String model, int year) {
		
		this.make = make;
		this.model = model;
		this.year = year;
	}
	
	public String getMake() {
		return this.make;
	}

	public void setMake(String make) {
		this.make = make;
	}
	
	public String toString() {
		return "My car is a " + this.year + " " + this.make + " " + this.model;
	}
}

Please note that I’ve removed the getters and setters for the other properties for brevity. Now, let’s create an instance of the car:

Car myCar = new Car("Aston Martin","DB9",2004);

Say we want to transmit the information stored in this object instance to a RESTful API, without having to map each individual property of our car to a corresponding field in the request. REST Assured supports this by allowing you to serialize the myCar object instance as follows:

@Test
public void testCarSerialization() {
				
	given().
		contentType("application/json").
		body(myCar).
	when().
		post("http://localhost:9876/carstub").
	then().
		assertThat().
		body(equalTo("Car has been stored"));
}

So, all you have to do is pass the object using body(). REST Assured will automatically translate this to the following request body:

{
    "make": "Aston Martin",
    "model": "DB9",
    "year": 2004
}

Neat, right? In this example, we serialized the car object to a JSON request, but REST Assured also allows you to serialize it to XML or HTML. Additionally, you can create custom object mappings as well. See this page in the REST Assured documentation for more information.

REST Assured also supports deserialization, meaning that we can easily transform a suitably formatted API response to a POJO instance:

@Test
public void testCarDeserialization() {
		
	Car myDeserializedCar = get("http://localhost:9876/carstub").as(Car.class);
		
	System.out.println(myDeserializedCar.toString());
	
	Assert.assertEquals("Check the car make", myDeserializedCar.getMake(), "Aston Martin");		
}

Note that http://localhost:9876/carstub points to a WireMock stub I’ve created to illustrate this example. The fact that our assertion is passing and that the console shows the following output when running the test tells us that deserialization has been successful:

My car is a 2004 Aston Martin DB9

You can download a Maven project containing all of the code I’ve used in the examples in this blog post here.

7 thoughts on “(De)serializing POJOs in REST Assured

  1. Pingback: Java Web Weekly, Issue 145 | Baeldung

  2. Pingback: Testing Bits – 10/2/16 – 10/8/16 | Testing Curator Blog

  3. I follow your instruction but i have an issue

    WHEN I post a request for authentication, the error occurs to say that: “java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded;charset=UTF-8”

    Here is my test method

    @Test
    public void authenticate()
    {
    AuthenDto authenDto = new AuthenDto(“username”,”password”,”false”,”Login”);
    given()
    .contentType(“application/x-www-form-urlencoded;charset=UTF-8”)
    .accept(“application/json, text/plain, */*”)
    .body(authenDto)
    .when()
    .post(“ENDPOINT”)
    .then()
    .statusCode(200);
    }

    • Not with the default included parsers. Don’t know if there’s an existing parser you could use, otherwise you’ll have to write and register your own I’m afraid.

  4. Team,

    I am facing same issue
    Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded
    how can we fix this problem

Leave a Reply

Your email address will not be published. Required fields are marked *