Creating data driven API tests with REST Assured and TestNG

As you’ve probably read in previous posts on this site, I am a big fan of the REST Assured library for writing tests for RESTful web services. In this post, I will demonstrate how to create even more powerful tests with REST Assured by applying the data driven principle, with some help from TestNG.

Combining REST Assured and TestNG
REST Assured itself is a Domain Specific Language (DSL) for writing tests for RESTful web services and 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 the latter offers an easy to use mechanism to create data driven tests through the use of the DataProvider mechanism.

Creating a TestNG DataProvider
A TestNG DataProvider is a method that returns an object containing test data that can then be fed to the actual tests (REST Assured tests in this case). This data can be hardcoded, but it can also be read from a database or a JSON specification, for example. It’s simply a matter of implementing the DataProvider in the desired way. The following example DataProvider creates a test data object (labeled md5hashes) that contains some example text strings and their md5 hash:

@DataProvider(name = "md5hashes")
public String[][] createMD5TestData() {
	return new String[][] {
			{"testcaseOne", "4ff1c9b1d1f23c6def53f957b1ed827f"},
			{"testcaseTwo", "39738347fb533d798aca9ae0f56ca126"},
			{"testcaseThree", "db6b151bb4bde46fddb361043bc3e2d9"}

Using the DataProvider in REST Assured tests (with query parameters)
Using the data specified in the DataProvider in REST Assured tests is fairly straightforward. In the example below, we’re using it in combination with a RESTful web service that uses a single query parameter:

@Test(dataProvider = "md5hashes")
public void md5JsonTest(String originalText, String md5Hash) {
		parameters("text", originalText).
		body("md5", equalTo(md5Hash));

Notice how the data in the DataProvider can be used for specification of expected results just as easy as for specifying input parameters. When we run this test, we can see in the output that the test is run three times, once for every set of input and validation parameters:

Console output for our data driven REST Assured test

Using the DataProvider in REST Assured tests (with query parameters)
The other parameter approach commonly used with RESTful web services is the use of path parameters. Let’s create another DataProvider, this one specifying Formula 1 circuits and the country they are situated in:

@DataProvider(name = "circuitLocations")
public String[][] createCircuitTestData() {
	return new String[][] {
			{"george","South Africa"}

The Ergast Developer API provides motor racing data through a public API (check it out, it’s really cool). Here’s how to parameterize the call to get specific circuit data and check the country for each circuit with REST Assured, again using our DataProvider:

@Test(dataProvider = "circuitLocations")
public void circuitLocationTest(String circuitId, String location) {

The only real difference is in how to set up path parameter use in REST Assured (see also here), otherwise making your tests data driven is just as easy as with query parameters.

When we run this second test, we can see again that it’s run three times, once for every circuit specified:

Console output for our data driven REST Assured test (with path parameters_

A Maven Eclipse project containing the code demonstrated in this blog post can be downloaded here.

40 thoughts on “Creating data driven API tests with REST Assured and TestNG

  1. Have you come across Spock? The Data Driven tests in Spock are real eye-candy. Spock coupled with REST Assured will produce some really useful and expressive tests.

  2. Pingback: Java Web Weekly, Issue 124 | Baeldung

  3. Pingback: Testing Bits – 5/8/16 – 5/14/16 | Testing Curator Blog

  4. Anybody can help me to figure out, how to generate some project for futher import in Postman (my team would like to use my testcases in postman and run it by themself with newman)

    • I have never really looked into Postman so far but I will try and do so this week. I’ll let you know if I find anything that might help you.

  5. Hi Bas,

    Thank you very much for this wonderful article! Do you have any idea about chaining multiple HTTP requests using rest assured? I want to use part of the response from an http request as a parameter for another HTTP request. I am not sure if restAssured has this kind of capability. Thank!

    • Hey Alim, REST Assured can certainly help you there. You can extract parts of the response (individual elements, but blocks of elements as well, pretty much everything that is the result of a GPath query) and reuse it in subsequent steps. See the documentation for the extract() method or my open source workshop (at for examples.

      • Thank you very much for your kind response Bas! it is really helpful. I just found out that for extensive api testing with very complex json files, it may be better to create java classes for such Json files and map the api response to that Java class using Jackson library together with Rest Assured.

        • Sounds like a decent approach indeed! I’ve written a post on deserializing API responses into POJOs as well, a quick search should point you to that article..

  6. How to do parametrization, when path params are not provided, only header in a get request(example: “/storeno/12232”), the number being the storeid. How to achieve parametrization in this?

    • Hi Moran, I’m not sure I understand your question. Headers are completely different from path parameters, but you can pass both to a REST Assured test through a variable.

      • Hi Mohan, something like this will do the job for you:
        int storeno=12232;
        String path = yourPath + /storeno/{storeno}

  7. Hi Bas,

    I need to create a data driven test using below requirements but dont know where to start. Can you please help me here. Below are the requirements

    Retrieve and persist a list of random cities distributed worldwide ( this number to consist of several thousand cities). Furthermore, create a framework capable of requesting weather, and currency/forex data using but not limited to open source API’s like:;;

    Create a data generator which builds on the city data previously retrieved. This tool should generate a dataset of randomised people (this set is expected to be in thousands), and populate the following fields: “Name, Surname, Address, City, Telephone, Email, Date of birth, Sex, Marital Status, Nationality, Country of birth, Gross annual income, and Currency”. This data must be built against standard regex patterns.

    • Is this a homework assignment? Or an assessment as part of a job interview? It does look that way.

      What have you tried yourself?

    • Out of principle I don’t help people with assignments like these.

      However, I suggest you find an API that can give you a city based on its zip code, because zip codes are numbers and therefore easy to randomize. Example: Or scrape a site like this:

      Build a list of cities and move from there.

      That’s all the help I am willing to give you.

  8. Pingback: REST API Testing – Software QA Automation

  9. Hi Bas,

    I need to parametrieze JSON body instead of query parameter or Path params. For example my josn body have an element “cityName”: “Boston”.
    While creating rest assured I convert JSON body to string. Now How can i parametrize params used in JSON body.


  10. Hi,
    Its a very useful page, i would like to ask another question to extend on the above scenarios.

    If my search returns an array list of customer objects (e.g 10 customer is returned) and I want to make sure each “data set” has the correct data.
    i dont want to check each field has so many times seperately. I want to check object 1 has those data, object 2 has those data.

    How can i ensure rest assurence checks each customer object seperately e.g:

    .body (“firstname[i],customerFirstname).

    or may be I cannot do it in the rest and I

  11. I have few questions.

    1. what is the difference between REST Assured and Test NG.

    2. if I am right, we can’t perform data driven testing using REST Assured ?

    3. how can I generate test reports in REST Assured framework?

    4. can I alone use REST Assured to complete my APIs automation ?

    • Hey Surendra, they’re entirely different things. TestNG (like JUnit) is a testing framework that provides a test runner, reporting, an assertion library and (among others) the option to create data driven tests.

      REST Assured on the other hand is a library that makes it easier to create tests for RESTful APIs. To create and run tests that use the power of REST Assured, you’ll need to use a test framework like TestNG or JUnit.

      There is no such thing as data driven testing in REST Assured, because that option is in the testing framework. The same goes for reporting.

      That also means that it’s a very good idea to combine the two.

  12. Here, we are creating data using data providers (createMD5TestData method) but still, this is going to be static.

    What is the best approach to generate params using map objects, data providers or JSON objects?

    If map or JSON object then how can we generate dynamic params?

    • Hey Nishant, by ‘static’, do you mean ‘hard coded’? In my view, ‘static’ means something entirely different in this context (static methods, for example).

      The problem with generating data is that you need to be very careful in generating the right data, you want your tests to be deterministic. You can generate some field values in a JSON object if you want, I’d recommend using the Java faker library for that. Another approach that might be interesting to you is property-based testing.

      There is no such thing as a ‘best approach’, though. Projects differ. Products differ. People differ. What works for me might not work for you.

  13. Great article. Can you please guide with file based parsing instead of data providers? For ex: dynamic data needs to be passed to API2.
    For ex:
    Id has to be passed from test.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.