How to validate XML response in Rest Assured?

We can validate XML response in Rest Assured. For obtaining an XML response, we have to pass the parameter ContentType.XML to the accept method. We shall first send a GET request via Postman on a mock API URL.

Using Rest Assured, we shall validate its XML Response containing the name of the subjects Rest Assured, Postman, and their prices 10 and 6 respectively.

In the above XML Response, we shall obtain the values of the name and price tags by traversing the paths - and courses.subject.price respectively.

We shall perform the assertion with the help of the Hamcrest framework which uses the Matcher class for assertion. To work with Hamcrest we have to add the Hamcrest Core dependency in the pom.xml in our Maven project. The link to this dependency is available in the below link −


Code Implementation

import org.hamcrest.Matchers;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class NewTest {
   void validateXMLResponse() {

      //base URI with Rest Assured class
      RestAssured.baseURI = "";

      //accept XML CONTENT

      //GET request .when().get("/55889581-

      //validate XML body

      //validate subject lists
      .body("", Matchers.hasItems
      ("Rest Assured", "Postman"))

      //validate price lists
      .body("courses.subject.price", Matchers.hasItems("10", "6"));


Updated on: 18-Nov-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started