Show List

Using XML Response

Below is a sample Java program demonstrating how to test a GET request of a REST API using JUnit and Rest Assured, expecting an XML response:


import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.junit.jupiter.api.Assertions.assertEquals; public class ApiGetWithXmlResponseTest { @Test public void testGetRequestWithXmlResponse() { // Base URI of the REST API RestAssured.baseURI = "https://api.example.com"; // Send GET request expecting XML response Response response = given() .accept("application/xml") // Set Accept header for XML response .get("/endpoint"); // Get and assert the response status code int statusCode = response.getStatusCode(); assertEquals(200, statusCode, "Expected status code 200"); // Get and print the response body String responseBody = response.getBody().asString(); System.out.println("Response Body:"); System.out.println(responseBody); } }

In this example:

  • We import JUnit's @Test annotation and assertEquals method for assertions.
  • We use the @Test annotation to mark the testGetRequestWithXmlResponse method as a test case.
  • We set the base URI of the REST API using RestAssured.baseURI.
  • We send a GET request to the /endpoint endpoint using given().accept("application/xml").get("/endpoint"), setting the Accept header to indicate that we expect an XML response.
  • We assert the response status code using JUnit's assertEquals method.
  • We retrieve and print the response body.

Ensure that you have Rest Assured, JUnit, and its dependencies added to your project's classpath. You can run this test case using JUnit test runner. Additionally, you may need to handle exceptions such as IOException or NullPointerException depending on your specific requirements and error handling strategy. Adjust the base URI and endpoint according to your API's structure.


    Leave a Comment


  • captcha text