From the course: Java: Automated API Testing with REST Assured

Making a GET request - Java Tutorial

From the course: Java: Automated API Testing with REST Assured

Start my 1-month free trial

Making a GET request

- [Instructor] Let's make a get request. As a reminder, a get request allows you to obtain information from the application. So, let's say we want to get all of the categories contained within our demo application. To do so, we make a request to this endpoint to read the categories using the get method. Let's invoke this using our testing library, rest-assured, but first we need to create a new class within a new package. So, we'll call our package trainingxyz and under there we'll create the new class and we'll call this ApiTests. Inside of our class, we'll create a new test method and we'll call this one getCategories. Inside of here we're going to use rest-assured to make the request. But first, let's discuss the pattern that rest-assured uses. Rest-assured follows the given, when, then pattern. Where you use given to specify any prerequisites to the actual request being made. This includes things like headers, authentication, or even setting parameters. Followed by when, which is where you describe the action to take. And finally, then, where you describe the expected result. So, we set this up as given. Notice we have a compilation error. That's okay. We just need to import this static method. With given, if there were any headers or parameters, this is where we would set them, but those are not required for this particular request so we move on to when. And remember, when is the action so this is where we make our get request by following the when with a get. Inside of the get call is where we pass our endpoint. So, let's make the end-point its own variable. And I'm just going to paste in that endpoint. And then we can pass this variable into the get. This is enough to make the request. Simple, right? But we also want to retrieve the response. To do so, we chain one more call here, which is then and that will return a response that we can validate. Now let's assign this validatable response to a variable which we'll call response. Great. That's all we need to make the request and store the response. Let's make one more get request which does require parameters. So, this will be a new test method and we'll call this one getProduct 'cause we're just going to get one specific product in this one. The endpoint to obtain the information about the product is the read one endpoint. So, I'll store that there and then let's set up our response. And we can go ahead without giving when then. Now, because this one is going to actually need to pass information this one will be a little bit longer. So, I'm going to start this one on a new line. So, we'll say given. Now, after our given, we call queryParam and we'll specify the ID parameter. Now, this method takes a key value pair. So, we will give it two arguments. The first one being ID followed by the value of the Idaho. To get the value of the ID, we can look in the database to find this value because the database will be our source of truth, meaning our API responses should match what's there. So, let's connect to the database. If you're using the community version of IntelliG, you'll need to go to phpMyAdmin to execute any queries. Here on the map page, click MySQL, and then click phpMyAdmin. Inside of phpMyAdmin, click the API testing DB and then click the SQL tab. Here you can execute SQL queries, such as select everything from products and then click Go. And notice the results displayed here. I'm using the ultimate edition of IntelliG which allows you to connect and query the database from right within the Idaho. For convenience, I'll use this feature to execute queries within the course, but again, if you do not have access to this feature, that's perfectly okay. You can still run the same queries within phpMyAdmin. For those with the ultimate version, let me show you how to connect to the database from IntelliG. So, we're going to click this database and we're going to say new and we're going to say, yep, a new source, and you're going to want to come down to MySQL. Now we need to fill in the details for this. To get those details, let's go back to the MAMP page. On our MAMP website let's scroll down to the MySQL section, click this and you'll see the information. So, our host is localhost. Our port is 8889. Username and password are here. So, let's go back and add those details to IntelliG. So, we have our host, localhost, and we say 8889. We give our username and our password and the database is called ApiTestingDB, the DB is capital. All right, let's test the connection. And great, that's connected, perfect. So we say, OK. Now, from here we have a console. So, we can select all of the products that are there. So, let's say select everything from products. And I'm going to click this arrow to execute it and notice here, we can see all of our products that are in the database. We can pick any of these IDs. Let's just pick ID two. So, we come back here. Let's close that. And we'll say two. All right. So, I have given this queryParam, what's next? We say when, right? So, I'm going to move this over here so you can really visualize it. So, given this queryParam, when we say get and we need to pass in our endpoints, then we get our response. So notice here, hopefully this indentation really helps you. So, given the queryParam of ID being two, when we call get, then we get a response. Okay, great. Now we have two get requests here. One that sends a parameter and one that does not. Now, let's run these. To do so, we can right click anywhere. If you do it right there on the test, it'll just run that one. So, let's go outside of that. Maybe here and say Run ApiTest. So, this will run both of those and notice they both are successful. However, there's no output on either one of these. So, what we'll do now is update both of these to print out the results, just so we can glance at the output with our eyes. To do so, we can add one more line to both of these tests. We have our response on the previous line so we can say response log body. And I'm going to copy and paste that into this other test as well. And let's run it again. And notice this time when we click on them, we actually see results. So, when we say we want the specific product, we see ID two, as well as all of the information for that product. When we click on get categories, we see all that the categories listed here. Great job. We successfully made two API requests using the get method. One that specify required data via parameters and another that did not require any additional data. Way to go.

Contents