You are on page 1of 13

1

https://www.nextgenerationautomation.com

Rest Assured
Coding Exercises

Prepared By:
Next Generation Automation Academy
Website: https://www.nextgenerationautomation.com

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
2

https://www.nextgenerationautomation.com

1. Write Code using Rest Assured API to verify API Response Code 200.

1. // Rest Assured Script to verify status code 200


2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest1 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void checkResponseCode_expect200() {
31. given().
32. spec(requestSpec).
33. when().
34. get("/us/90210").
35. then().
36. assertThat().
37. statusCode(200);
38. }
39. }

Here is JSON file to simulate the API

1. {
2. "request": {
3. "method": "GET",
4. "url": "/us/90210"
5. },
6. "response": {
7. "status": 200,
8. "headers": { "Content-Type": "application/json" },
9. "jsonBody": {"post code": "90210","country": "United States","country abbrevia-
tion": "US",
10. "places": [{"place name": "Beverly Hills","longitude": "-118.4065",
11. "state": "California","state abbreviation": "CA",
Copyright @ 2020 Next Generation Automation
Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
3

https://www.nextgenerationautomation.com
12. "latitude": "34.0901"}]}
13. }
14. }

2. Write Code using Rest Assured API to verify API Response Code 404

1. // Rest Assured Script to verify status code 404


2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest2 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void checkResponseCode_expect404() {
31. given().
32. spec(requestSpec).
33. when().
34. get("/us/99999").
35. then().
36. assertThat().
37. statusCode(404);
38. }
39. }

Here is JSON file to simulate the API

1. {
2. "request": {
3. "method": "GET",
4. "url": "/us/99999"
5. },
6. "response": {
7. "status": 404,
8. "headers": { "Content-Type": "application/json" },
9. "jsonBody": {"error": "zip code 99999 does not exist for country code us"}

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
4

https://www.nextgenerationautomation.com
10. }
11. }

3. Write Code using Rest Assured API to verify Content Type of API response

1. // Rest Assured Script to verify content type


2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest3 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void checkContentType_expectApplicationJson() {
31. given().
32. spec(requestSpec).
33. when().
34. get("/us/90210").
35. then().
36. assertThat().
37. contentType(equalTo("application/json"));
38. }
39. }

Use JSON File as mentioned in Exercise 1 to simulate the API response.

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
5

https://www.nextgenerationautomation.com

Register Now

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
6

https://www.nextgenerationautomation.com
4. Write Code using Rest Assured API to verify AP Response Body Contents

1. // Rest Assured Script to verify body contents


2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest4 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void requestUsZipCode90210_checkStateForFirstPlace_expectCalifornia() {
31. given().
32. spec(requestSpec).
33. when().
34. get("/us/90210").
35. then().
36. assertThat().
37. body("places[0].state", equalTo("California"));
38. }
39.
40. @Test
41. public void extractCountryFromResponse() {
42. String actualCountry =
43. given().
44. spec(requestSpec).
45. when().
46. get("/us/90210").
47. then().
48. extract().
49. path("country");
50.
51. Assert.assertEquals("United States", actualCountry);
52. }
53. }

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
7

https://www.nextgenerationautomation.com
Use JSON File as mentioned in Exercise 1 to simulate the API response.

5. Write Code using Rest Assured API to perform POST Operation using Model Class
and check API Response code 200

1. // Rest Assured Script to perform post operation using model class and check status code 200
2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest5 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void postCarObject_checkResponseHttpStatusCode_expect200() {
31. Car myCar = new Car("Ford", "Focus", 2012);
32. given().
33. spec(requestSpec).
34. and().
35. body(myCar).
36. when().
37. post("/car/postcar").
38. then().
39. assertThat().
40. statusCode(200);
41. }
42. }

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
8

https://www.nextgenerationautomation.com
Model class to use as below:

1. package nextGenerationAutomationLearnRestAssured;
2.
3. public class Car {
4.
5. private String make;
6. private String model;
7. private int modelYear;
8.
9. public Car() {
10.
11. make = "";
12. model = "";
13. modelYear = 0;
14. }
15.
16. public Car(String make, String model, int modelYear) {
17.
18. this.make = make;
19. this.model = model;
20. this.modelYear = modelYear;
21. }
22.
23. public void setMake(String make) { this.make = make; }
24.
25. public void setModel(String model) { this.model = model; }
26.
27. public void setModelYear(int modelYear) { this.modelYear = modelYear; }
28.
29. public String getMake() { return this.make; }
30.
31. public String getModel() { return this.model; }
32.
33. public int getModelYear() { return this.modelYear; }
34. }

Use JSON File as below to simulate the API response.

1. {
2. "request": {
3. "method": "POST",
4. "url": "/car/postcar",
5. "bodyPatterns":[{
6. "equalToJson": { "make": "Ford", "model": "Focus", "modelYear": 2012 }
7. }]
8. },
9. "response": {
10. "status": 200,
11. "headers": { "Content-Type": "application/json" },
12. "jsonBody": { "result": "OK" }
13. }
14. }

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
9

https://www.nextgenerationautomation.com
6. Write Code using Rest Assured API to perform POST Operation and check contents
of Response Body using Model Class

1. // Rest Assured Script to check contents of response body using model class
2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.*;
12. import org.junit.*;
13.
14. public class RestAssuredTest5 {
15.
16. private static RequestSpecification requestSpec;
17.
18. @Rule
19. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
20.
21. @BeforeClass
22. public static void createRequestSpecification() {
23. requestSpec = new RequestSpecBuilder().
24. setBaseUri("http://localhost").
25. setPort(9876).
26. build();
27. }
28.
29. @Test
30. public void getCarObject_checkModelYear_expect2016() {
31. Car myCar = given().
32. spec(requestSpec).
33. when().
34. get("/car/getcar/alfaromeogiulia").
35. as(Car.class);
36.
37. Assert.assertEquals(2016, myCar.getModelYear());
38. }
39. }

Use JSON File as below to simulate the API response.

1. {
2. "request": {
3. "method": "GET",
4. "url": "/car/getcar/alfaromeogiulia"
5. },
6. "response": {
7. "status": 200,
8. "headers": { "Content-Type": "application/json" },
9. "jsonBody": { "make": "Alfa Romeo", "model": "Giulia", "modelYear": 2016 }
10. }
11. }

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
10

https://www.nextgenerationautomation.com
Use Model Class as shown in Exercise 5.

7. Write Code using Rest Assured API to perform Data driven testing and verify contents
of Response Body

1. // Rest Assured Script to perform data driven testing and verify contents of response body
2.
3. package nextGenerationAutomationLearnRestAssured;
4.
5. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
6. import static io.restassured.RestAssured.*;
7. import static org.hamcrest.Matchers.*;
8.
9. import com.github.tomakehurst.wiremock.junit.WireMockRule;
10. import io.restassured.builder.RequestSpecBuilder;
11. import io.restassured.specification.RequestSpecification;
12. import org.junit.*;
13. import org.junit.runner.RunWith;
14. import com.tngtech.java.junit.dataprovider.*;
15.
16. public class RestAssuredTest5 {
17.
18. private static RequestSpecification requestSpec;
19.
20. @Rule
21. public WireMockRule wireMockRule = new WireMockRule(options().port(9876));
22.
23. @BeforeClass
24. public static void createRequestSpecification() {
25. requestSpec = new RequestSpecBuilder().
26. setBaseUri("http://localhost").
27. setPort(9876).
28. build();
29. }
30.
31. @DataProvider
32. public static Object[][] zipCodeData() {
33. return new Object[][] {
34. { "us", "90210", "California" },
35. { "us", "12345", "New York" },
36. { "ca", "Y1A", "Yukon" }
37. };
38. }
39.
40. @Test
41. @UseDataProvider("zipCodeData")
42. public void checkStateForCountryCodeAndZipCode(String countryCode, String zip-
Code, String expectedState) {
43. given().
44. spec(requestSpec).
45. and().
46. pathParam("countryCode", countryCode).
47. pathParam("zipCode", zipCode).
48. when().
49. get("/{countryCode}/{zipCode}").
50. then().
Copyright @ 2020 Next Generation Automation
Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
11

https://www.nextgenerationautomation.com
51. assertThat().
52. body("places[0].state",equalTo(expectedState));
53. }
54. }

Use JSON File as below to simulate API Response along with Exercise 1 JSON:

US12345.json

1. {
2. "request": {
3. "method": "GET",
4. "url": "/us/12345"
5. },
6. "response": {
7. "status": 200,
8. "headers": { "Content-Type": "application/json" },
9. "jsonBody": {"post code": "12345","country": "United States","country abbrevia-
tion": "US",
10. "places": [{"place name": "Schenectady","longitude": "-
118.4065","state": "New York",
11. "state abbreviation": "NY","latitude": "34.0901"}]}
12. }
13. }

CA1YA.json

1. {
2. "request": {
3. "method": "GET",
4. "url": "/ca/Y1A"
5. },
6. "response": {
7. "status": 200,
8. "headers": { "Content-Type": "application/json" },
9. "jsonBody": {"post code": "Y1A","country": "Canada","country abbreviation": "CA",
10. "places": [{"place name": "Whitehorse","longitude": "-
118.4065","state": "Yukon",
11. "state abbreviation": "YT","latitude": "34.0901"}]}
12. }
13. }

#NGAutomation
Building better for tomorrow

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
12

https://www.nextgenerationautomation.com

Register Now

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.
13

https://www.nextgenerationautomation.com

Thanks!!

Next Generation Automation Academy

Copyright @ 2020 Next Generation Automation


Information shared for internal consumption by Automation QAs and
Does not hold any commercial value.

You might also like