Skip to main content

Data Driven API Tests with REST Assured and TestNG

REST Assured itself is a Domain Specific Language (DSL) for writing tests for RESTful web services and REST Assured does not offer a mechanism for writing data driven tests (i.e., the ability to write a single test that can be executed multiple times with different sets of input and validation parameters). However, REST Assured tests are often combined with JUnit or TestNG, and offers an easy to use mechanism to create data driven tests through the use of the DataProvider mechanism.

Consider this example:

/*POST Request Example*/
@Test(enabled=true)
public void postExample()
{
    Response res = 
        given()
            .header("Content-Type","application/json")
            .body("{\n" + 
               "    \"name\": \"Tarun Goswami1\",\n" + 
               "    \"job\": \"QA\"\n" + 
               "}")
            .when()
                .post("/users")
            .then()
                .statusCode(201)
                .extract().response();
		
     res.body().prettyPrint();
}

Here you can see in body section we are creating post request for User Tarun Goswami with role QA. Suppose you want to do it for multiple user. In that case you can use Data Provider. You need to change your code like this:

@DataProvider(name = "userlogin")
public String[][] createUser() 
{			
    return new String[][]
    {
        {"Tarun Goswami","QA"},
        {"Ram Sharma","DEV"},
        {"Mohan Verma","SM"}
    };
}
	
/*POST Request Example*/
@Test(enabled=true, dataProvider ="userlogin")
public void postExample(String name, String profile)
{
    Response res = 
    given()
        .header("Content-Type","application/json")
        .body("{\n" + 
            "    \"name\": \""+name+"\",\n" + 
            "    \"job\": \""+profile+"\"\n" + 
        "}")
    .when()
        .post("/users")
    .then()
        .statusCode(201)
        .extract().response();
		
    res.body().prettyPrint();
}

A Data Provider is a method on your class that returns an array of array of objects.  This method is annotated with @DataProvider.

Data provider returns a two-dimensional JAVA object to the test method,  An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. For example, if the DataProvider returns an array of 3*2 objects, the corresponding testcase will be invoked 3 times with 2 parameters each time. Similar is the case with above example. 

A @Test method specifies its Data Provider with the dataProvider attribute.  This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.

By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute.

@Test(dataProvider = "userlogin", dataProviderClass = StaticProvider.class)

Submitted by tgoswami on January 4, 2021

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.