├── .gitignore ├── README.md ├── TestNG.xml ├── pom.xml └── src └── test └── java ├── BaseTest.java ├── BasicApiTest.java └── util ├── RestAssuredUtil.java └── TestUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | .idea 15 | *.jar 16 | *.war 17 | *.nar 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | target 23 | *.iml 24 | 25 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 26 | hs_err_pid* 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # api-automation-rest-assured-basic 2 | Basic Rest Assured Automation Example 3 | 4 | You can learn details in swtestacademy.com 5 | Here is the link: https://www.swtestacademy.com/api-testing-with-rest-assured/ 6 | -------------------------------------------------------------------------------- /TestNG.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | api-automation-basic 8 | api-automation-basic 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 4.4.0 13 | 14 | 15 | 16 | 17 | org.testng 18 | testng 19 | 7.4.0 20 | test 21 | 22 | 23 | 24 | io.rest-assured 25 | rest-assured 26 | ${rest-assured-version} 27 | test 28 | 29 | 30 | io.rest-assured 31 | rest-assured 32 | ${rest-assured-version} 33 | compile 34 | 35 | 36 | 37 | io.rest-assured 38 | json-path 39 | ${rest-assured-version} 40 | 41 | 42 | 43 | io.rest-assured 44 | json-schema-validator 45 | ${rest-assured-version} 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-compiler-plugin 54 | 55 | 11 56 | 11 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /src/test/java/BaseTest.java: -------------------------------------------------------------------------------- 1 | import io.restassured.http.ContentType; 2 | import io.restassured.path.json.JsonPath; 3 | import io.restassured.response.Response; 4 | import org.testng.annotations.AfterClass; 5 | import org.testng.annotations.BeforeClass; 6 | import util.TestUtil; 7 | 8 | public class BaseTest { 9 | 10 | public Response res = null; //Response 11 | public JsonPath jp = null; //JsonPath 12 | 13 | //Instantiate a Helper Test Methods (testUtils) Object 14 | TestUtil testUtil = new TestUtil(); 15 | 16 | @BeforeClass 17 | public void setup() { 18 | //Test Setup 19 | utils.RestAssuredUtil.setBaseURI(); //Setup Base URI 20 | utils.RestAssuredUtil.setBasePath("api"); //Setup Base Path 21 | utils.RestAssuredUtil.setContentType(ContentType.JSON); //Setup Content Type 22 | } 23 | 24 | @AfterClass 25 | public void afterTest() { 26 | //Reset Values 27 | utils.RestAssuredUtil.resetBaseURI(); 28 | utils.RestAssuredUtil.resetBasePath(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/BasicApiTest.java: -------------------------------------------------------------------------------- 1 | import org.testng.annotations.Test; 2 | 3 | public class BasicApiTest extends BaseTest { 4 | 5 | @Test 6 | public void T01_StatusCodeAndGetClientsTest() { 7 | res = utils.RestAssuredUtil.getResponse("/gen/clients"); 8 | testUtil.checkStatusIs200(res); 9 | jp = utils.RestAssuredUtil.getJsonPath(res); 10 | System.out.println(testUtil.getClients(jp)); 11 | } 12 | 13 | @Test 14 | public void T02_GetAndroidModelPackageOptions() { 15 | res = utils.RestAssuredUtil.getResponse("/gen/clients/android"); 16 | testUtil.checkStatusIs200(res); 17 | jp = utils.RestAssuredUtil.getJsonPath(res); 18 | System.out.println("Opt: " + jp.get("modelPackage.opt")); 19 | System.out.println("Description: " + jp.get("modelPackage.description")); 20 | System.out.println("Type: " + jp.get("modelPackage.type")); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/util/RestAssuredUtil.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import static io.restassured.RestAssured.given; 4 | 5 | import io.restassured.RestAssured; 6 | import io.restassured.http.ContentType; 7 | import io.restassured.path.json.JsonPath; 8 | import io.restassured.response.Response; 9 | 10 | public class RestAssuredUtil { 11 | //Sets Base URI 12 | public static void setBaseURI() { 13 | RestAssured.baseURI = "http://generator.swagger.io/"; 14 | } 15 | 16 | //Sets base path 17 | public static void setBasePath(String basePathTerm) { 18 | RestAssured.basePath = basePathTerm; 19 | } 20 | 21 | //Reset Base URI (after test) 22 | public static void resetBaseURI() { 23 | RestAssured.baseURI = null; 24 | } 25 | 26 | //Reset base path 27 | public static void resetBasePath() { 28 | RestAssured.basePath = null; 29 | } 30 | 31 | //Sets ContentType 32 | public static void setContentType(ContentType Type) { 33 | given().contentType(Type); 34 | } 35 | 36 | //Returns response by given path 37 | public static Response getResponse(String path) { 38 | return given().get(path); 39 | } 40 | 41 | //Returns response 42 | public static Response getResponse() { 43 | return given().get(); 44 | } 45 | 46 | //Returns JsonPath object 47 | public static JsonPath getJsonPath(Response res) { 48 | String json = res.asString(); 49 | return new JsonPath(json); 50 | } 51 | } -------------------------------------------------------------------------------- /src/test/java/util/TestUtil.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import io.restassured.path.json.JsonPath; 4 | import io.restassured.response.Response; 5 | import java.util.ArrayList; 6 | import org.testng.Assert; 7 | 8 | public class TestUtil { 9 | //Verify the http response status returned. Check Status Code is 200? 10 | public void checkStatusIs200(Response res) { 11 | Assert.assertEquals(res.getStatusCode(), 200, "Status Check Failed!"); 12 | } 13 | 14 | //Get Clients 15 | public ArrayList getClients(JsonPath jp) { 16 | return jp.get(); 17 | } 18 | } 19 | --------------------------------------------------------------------------------