Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

How to Implement Java Rest API Automation using RestAssured?

Introduction to Java APIs

When we talk about Java APIs, there are different ways to create this, apps are some set of standards we need to follow to create this app. Which developer does that? So, the most common apps with everyone users are restabilized less to possess some rules and regulations in creating an API so that everyone needs to follow those criteria to fall under the rest umbrella.

Similarly, there are APIs, Castle Beeb's. We also see them as Web services. Services are API. These terms can be interchangeable.

There is a slight difference, but usually, people call APIs or services by the same name. So, Services use a different standard in defining that some services more stuff. Legacy services are based on SOAP as the rest are ones on the market a few years ago. So, services are in existence from it. The problem here is that SOAP has so many conditions, so many restrictions and that that that provides tight authorization authentication as well to communicate with APIs even less the same. But we call it a lightweight protocol.

Basics of Rest Assured and Rest APIs

And now, most web applications are moving towards restabilized simply because of their lightness and flexible way of coding. This blog is entirely focused on RestAssured. Only the legacy services which are in existence for a year are still on social services. But all the latest technologies like Facebook, Google, Instagram, every other website uses less database only because it's easy to build and maintain.

That's the main reason. And less testing is very, very necessary because now for Hotels.com guy. He needs to know whether he is getting the actual result from the Marriott APIs or not. So, that's not possible to talk to create reservation matter because that is an external third-party coordinate. What he can do is test the matter a bit to see if he is getting the proper response whenever the Marriott Hotel is sending back.

If there is something wrong with this API, it is clear that the wrong details will be returned to the customer after they book the ticket. And if someone is not thoroughly testing the CPA, and that's it and they might get a random number, or they may not see the reservation correctly and there will be so many problems.

Some people can test this back in technology in Marriott Company. And some people test this front in part to see if everything in the U.S. is working fine. But if you don’t test the decomposition part of how you get the data, then, you know your app can end up with a lot of big bugs. So that's the reason these days, testing and APIs are becoming the hardest requirement are tested on a lighter weight.

Request Flow with Rest APIs

java rest api automation

You just need to hit an endpoint and you will get the response within a second. You will be able to tell if Marriott apps are working as expected or not. OK. And before we start with an example, we need to understand some terminologies as part of this stapes. OK. So, as I said, REST is one of the architectural designs to create a.

These are some of the rules which you have to follow. Step one is an endpoint, what these endpoints are, we can also say to the base you ordered. So, this is the place where your AIPA is hosted. And now if you want to hit this AIPA, you need to hit that endpoint. So End Point is that area where you are API will like your API will seek it there and someone asks you to hit that API, which is nothing but hit that endpoint, hit that base you are to exactly that Sarovar is located.

Understanding the Crud Operations of API

So, if you want to test any API, the first requirement that you need to have is an endpoint that bases URI Exactly. That the base is located so that you can call that the API by hitting that endpoint from your API testing tools. Step one, Java Spring Boot Developers, will clearly explain, what the point is where to give it, how to hit it, how to get the response, everything you learn, and just giving your high-level overview before we start. And next, we all know that. Information is being sent to API on HTTP protocol.

Different methods have to be exposed when you are communicating with the rest APIs from your client. So, one of the methods is to get then post, put and delete, these are the four commonly used methods to communicate with the rest of APIs. So here, when you talk to APIs, you can talk it to get his similar or post or delete or put. So, let's try to understand on a high level every history department. And now so that while going forward, when we start taking Real-Time examples, we will do some examples on all these HTTP methods.

Note: You can read our other blogs to understand more about HTTP Methods and the differences between them.

Request/Response example of an Rest API

Category Description
URI https://www.example.com/employee/enroll/v1/{Id}/register
Method PUT
Request Headers
Header Name Description Value / Value Format Required
Content-Type Content Type being passed application/json Yes
Authorization Key and PIN Info Encrypted (using SHA256) and Base-64 encoded value Yes
clientAuth Combination of Secret IDs Combination of : yes
AUTH_TYPE This header is required for the Auth security features key-sha256 yes
Request Headers Sample: Content-Type: application/json
Authorization: M25CQXphMXlMMHlwTEFNMUNQZDRYRjdaUXA=
clientAuth: examplekey-sha256
AUTH_TYPE: knynYa1KrMLgt0L1F6r83U2TcqdEeqqyHd5Dy6w9DgEHK83UuRjilKu00+n3T6RbHeOvwFpGxm9KJqlC1rhw==
Request Body {
"certSigningRequest":"XHYYICRTCCAS0CAQAwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMYQeanwwNRDa9oK0fNdZ1Zn7B46MA4i6Xv3EOc+MELwzwQjkcIYEiupYwkson4HjYNgsn06HL2wDiH0VtI/rLrV+ /Ctqw",
"ipAddresses":"127.0.0.1;172.17.0.2",
"version" : "0.0.0.1"
}
Response {
"APIKey": "e75d45a2a4c80f00cc5a37e8372a145a2d9224bba3a43a253fd6e959c8901e528cb2325e787636032c8f47086cf1163a062c2389923de68b6a7d48f0984e5534",
"certificateResponse": "-----BEGIN CERTIFICATE-----\nXGTHNUCCBGqgAwIBAgITSwAAY2JvubxE02+tnAAAAABjYjANBgkqhkiG9w0BAQsF\nADCBrzELMAkGA1UEBhMCVVMxDjAMBgNVBAgTBVRleGFzMRMwEQYDVQQHEwpSb3Vu\nZCBSb2NrMR8wHQYDVQQKExZEZWxsIFRlY2hub2xvZ2llcyBJbmMuMSQwIgYDVQQL\nExtEZWxsIFNlY3VyZSBSZW1vdGUgU2VydmljZXMxNDAyBgNVBAMTK0RlbGwgU2Vj\ndXJlIFJlbW90ZSBTZXJ2aWNlcyBUZXN0IElzc3VpbmcgQ0EwHhcNMjAwMTEzMDgx\nMjU2WhcNNDUwMTA2MDgxMjU2WjCBqTELMAkGA1UEBhMCVVMxDjAMBgNVBAgTBVRl\neGFzMRMwEQYDVQQHEwpSb3VuZCBSb2NrMR8wHQYDVQQKExZEZWxsIFRlY2hub2xv\nZ2llcyBJbmMuMSQwIgYDVQQLExtEZWxsIFNlY3VyZSBSZW1vdGUgU2VydmljZXMx\nLjAsBgNVBAMMJVNFUlZJQ0UgUExBTk5JTkcgLSBTVlRfRUxNU0FFMDEyMDI0VzAw\nggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDGEHmp8MDUQ2vaCtHzXWdW\nZ+weOjAOIul79xDnPjBC8M8EI5HCGBIrqWMJLKJ”,
"UniqueNumber": " UNAEAE07208LD7 ",
"siteInfo": {
"contactDetails": {
"firstName": "Aishwariya",
"lastName": "Prakash",
"email": "[email protected]",
}
}

Build Rest API Automation Test

So now, let's try to write our first automation test, so here I will show how to automate our register Employee API first. That is our goal. Validate if our place API is working as expected. So, this is our first requirement. Now, rest assured, works on three principles "given, when and then". What all details you are giving for an API, should be wrapped under the given method. There are three methods we are using in our everyday automation test. The given method will take all the input details, what you need to submit for the API, and once you want to hit and submit the API with all the details in the given method, you will go into the method and submit that specific API and there The second method will be called.

In this then method, you will validate the response. So this is the principle of rest assured, rest assured, all work with given when and then so that anyone who monitors the script will understand what all the input details are given to that API and how we are submitting that API. Are we submitting with getting HTTP requests are post HTTP request those details will go in when and then what all validations you are doing on the response that API got generated? That validation will go under the Method. We will do an example to understand all these three methods. So now in this registerEmployee API, let’s try to write our restAssured test based on these three principles.

Implementation of RestAssured Automation:

So, any test, any rest assured automation test should be written in these three principles only. The first one is a given right. But before that, you have to set your base URL.

RestAssured.baseURI =”www.example.com”;

So first, you must set your best URI. In our case-based you are nothing but rather example.com So this is our best worry. You can first give that information to your automation test. And now we will wrap all of our rest automation into these three methods. First, let's start with giving. Given that so you need to import one package, but when you scroll your mouse pointer, you will not get any addition to import because this package is static. You have to manually write that package name on the top (import io.restassured.RestAssured).

What it got important when you write this, rest assured, the only thing you have to do is static. So, you just need to put static keyword because there are some packages and rest assured which are static (import static io.restassured.RestAssured.*). All those static packages will be automatically imported when you use the static import statement and given belongs to that RestAssured static package.

Sample Use Case Implementation using RestAssured Automation

You need not remember anything when you're right. Rest assured. So, let's say you don't have anything here. So, when you hover your mouse to rest assured, it will ask you to import this Rest assured. Once You got this package right, then you are good to go. Just copy this package and it and add one more static keyword and then give “.*” So, that it will pull out all the packages present in RestAssured. That's a simple shortcut on importing this given metal package.

java rest api 2

So, given so what all you have now, you should start giving step one. Let's start giving very parameters. We are having one pretty parameter which we are sending to our request. So simply we need to start by header and then you will get one method that will ask you to enter header attributes like below:

java rest api 3

Here, we are having content-type applications last as our header. Select the required methods and make sure you write them in the double quotes for all the header fields. And next, what is the request body and how to pass it through Rest Assured?

So, we are telling that body what we are sending is in a different format. Right, because I thought that all in detail should be part of the body. Right, So you start with a given dot, you have a body parameter and then you give one more dot so that you will get the third thing i.e. equalTo() method where we have to compare the value against the response body. The response I will talk about later. Below is how to pass the request body in Rest Assured.

java rest api 4

So, this is the body you are sending to your APIs, right? Here, you have to pass through the entire body inside this, so this is your body. But this is the complete string, right, this body that accepts key and value pair and body as a startling format. So this resource will get concatenated with the screenshot and it will use all these details and then it submits.

java rest api 5

You can see from the below image that we have to pass the request method and in our case, it is “PUT” and then inside the put method I have passed our end point URL so that the request will get executed by the RestAssured client.

Now, after it submits, we need to validate the response rate. Any response validation will go under. Then method, From the next line, again, you can say dot and then and now you can start validations chaining to this then method. Here, let’s do basic validation, where we want to validate whether the status code is 200 or not. If this API is a success, then you will get a status score of 200. So, this is the first validation let us do to see if this API went through successfully or not.

.then().log()
.ifValidationFails().statusCode(200).body(matchesJsonSchemaInClasspath("Response.j son"))

You can have assumptions. You see them as something that so that means you can check all assertions so that if assertions do not match, your test will fail. Here in our case for doing the proper validation, what you can do is you have to compare the response body data with the excepted result so that your test case will pass on that basis.

java rest api 6

To achieve this, RestAssured provides us the combination of .body() and equal To() method. The response body received from the API call can be compared with the equalTo() method and if all the value matches with the excepted results then the testcase will pass otherwise it will fail. Please see the example I have given in the screenshot above.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients