top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Data Driven Testing in Rest API using single Json file in Java

In this blog, we will see how we achieved using a single Json file to provide input values to a Rest API. To understand this blog better, readers should have prior basic knowledge of Rest Assured, like How to Create, Post, Update, Delete requests using Rest API in Java and basic of Java.

What is Data Driven Testing?

Data Driven Testing is very significant for Test Automation. Data Driven testing framework is an Automation testing framework in which input values for a test scenario/method are read from an external source and then stored in variables. This external source can be a file (excel, xml, Json, csv etc.) or a database. Multiple external sources can be used at any time in a framework.

Advantages:

  • No hard coded input values in the test code or request body.

  • One test script can be executed multiple times for multiple scenarios with different test data. So, it reduces the code and increasing code re-usability.

Now we will see how to read a data from the Json file with sample requirement.

Preconditions :

1. Create a Maven Project in eclipse.

2. Add dependency in POM.xml.

3. Create a Json file with required input data.

Dependency:-

Sample Json file with input data:-

Here ,Each Scenario [both positive and negative] is refered with KEY.

Eg:-PateintCreationValid is a key for create a patient record with valid input data.

Now we start with the implementation with sample business requirement.

Requirement: User able to create a patient record by sending POST request with required input fields in the Json format.

Sample Request Body:


Validation: Auto generated patient ID and Auto generated patient file id along with status code 201.

Implementation:

Here, I am creating a method for sending a POST request to create a patient record by reading the input value from the Json file.

In below code, the method setup_PatientCreation() is for creating the request.

PatientCreation() is to send the request.

Note:

“String E_mail=jsonObj.getJSONObject(Key).getString(“Email”).toString();” Use this code in PatientCreation() method to fetch the email id sent in the request .

Here, The Jackson ObjectMapper class (com.fasterxml.jackson.databind.ObjectMapper) is the simplest way to parse JSON with Jackson in Java. The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON.The Jackson ObjectMapper can also create JSON from Java objects. Generating JSON from Java objects is also referred to as to serialize Java objects into JSON.

Above is the sample code for validation. It fetches the autogenerated patient id and file id from the response. Also we validating the email from the request and response.

Conclusion — With this Approach, we can run one request for multiple scenarios, which can be a combination of positives and negatives too. Also, we can make sure that there are no hard codes in our project, which is always a good practice.

4 views0 comments

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
bottom of page