├── .gitignore
├── .idea
├── .gitignore
├── compiler.xml
├── jarRepositories.xml
├── misc.xml
└── vcs.xml
├── LICENSE
├── README.md
├── pom.xml
├── src
└── test
│ └── java
│ └── requestRes.java
└── target
└── test-classes
└── requestRes.class
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 |
9 | # Diagnostic reports (https://nodejs.org/api/report.html)
10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11 |
12 | # Runtime data
13 | pids
14 | *.pid
15 | *.seed
16 | *.pid.lock
17 |
18 | # Directory for instrumented libs generated by jscoverage/JSCover
19 | lib-cov
20 |
21 | # Coverage directory used by tools like istanbul
22 | coverage
23 | *.lcov
24 |
25 | # nyc test coverage
26 | .nyc_output
27 |
28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29 | .grunt
30 |
31 | # Bower dependency directory (https://bower.io/)
32 | bower_components
33 |
34 | # node-waf configuration
35 | .lock-wscript
36 |
37 | # Compiled binary addons (https://nodejs.org/api/addons.html)
38 | build/Release
39 |
40 | # Dependency directories
41 | node_modules/
42 | jspm_packages/
43 |
44 | # TypeScript v1 declaration files
45 | typings/
46 |
47 | # TypeScript cache
48 | *.tsbuildinfo
49 |
50 | # Optional npm cache directory
51 | .npm
52 |
53 | # Optional eslint cache
54 | .eslintcache
55 |
56 | # Microbundle cache
57 | .rpt2_cache/
58 | .rts2_cache_cjs/
59 | .rts2_cache_es/
60 | .rts2_cache_umd/
61 |
62 | # Optional REPL history
63 | .node_repl_history
64 |
65 | # Output of 'npm pack'
66 | *.tgz
67 |
68 | # Yarn Integrity file
69 | .yarn-integrity
70 |
71 | # dotenv environment variables file
72 | .env
73 | .env.test
74 |
75 | # parcel-bundler cache (https://parceljs.org/)
76 | .cache
77 |
78 | # Next.js build output
79 | .next
80 |
81 | # Nuxt.js build / generate output
82 | .nuxt
83 | dist
84 |
85 | # Gatsby files
86 | .cache/
87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js
88 | # https://nextjs.org/blog/next-9-1#public-directory-support
89 | # public
90 |
91 | # vuepress build output
92 | .vuepress/dist
93 |
94 | # Serverless directories
95 | .serverless/
96 |
97 | # FuseBox cache
98 | .fusebox/
99 |
100 | # DynamoDB Local files
101 | .dynamodb/
102 |
103 | # TernJS port file
104 | .tern-port
105 |
106 | .idea
107 | .DS_Store
108 | .idea/compiler.xml
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Alphabin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # api-rest-assured-sample
2 |
3 | Used tools and frameworks
4 | ---------------------------------------
5 | 1. Rest-Assured
6 | 2. Maven repository
7 | 3. TestNG
8 |
9 | ## How to create Rest API test framework using Rest Assured Java library
10 |
11 | • Maven Dependency to add,
12 |
13 |
14 | io.rest-assured
15 | rest-assured
16 | 5.0.0
17 | test
18 |
19 |
20 | • RestAssured.uri = "" // to specify the basic URL of the API
21 |
22 | • Basic building blocks of Rest Assured is - given(), when(), then()
23 |
24 | • Values that can be provided in the given block,
25 | Response res = given().
26 | baseUri("https://reqres.in/api/users?page=2").
27 | contentType(ContentType.JSON).
28 |
29 | • To print the response body as string,
30 | System.out.println(res.getBody().asString());
31 |
32 | • To specify particular header value when sending the request
33 | Header header = new Header("key","value");
34 | RequestSpecification requestSpecification = given().header(header);
35 |
36 | • To validate response content type,
37 | response.then().contentType(ContentType.JSON);
38 |
39 | • To get log when response is being fetched,
40 | response.then().log().all();
41 |
42 | • To validate a particular item from the response body and status code,
43 | Assert.assertEquals(res.statusCode(),200);
44 |
45 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | ReqRes
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | io.rest-assured
14 | rest-assured
15 | 4.4.0
16 | test
17 |
18 |
19 |
20 | org.testng
21 | testng
22 | 7.5
23 | test
24 |
25 |
26 |
27 | 17
28 | 17
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/test/java/requestRes.java:
--------------------------------------------------------------------------------
1 | import io.restassured.http.ContentType;
2 | import io.restassured.response.Response;
3 | import org.testng.Assert;
4 | import org.testng.annotations.Test;
5 | import java.util.HashMap;
6 | import static io.restassured.RestAssured.given;
7 | public class requestRes {
8 |
9 | @Test
10 | public void listUsers(){
11 | Response res = given().
12 | baseUri("https://reqres.in/api/users?page=2").
13 | contentType(ContentType.JSON)
14 | .when()
15 | .get();
16 | Assert.assertEquals(res.statusCode(),200);
17 | System.out.println(res.getBody().asString());
18 | }
19 |
20 | @Test
21 | public void singleUsers(){
22 | Response res = given().
23 | baseUri("https://reqres.in/api/").
24 | contentType(ContentType.JSON).
25 | when()
26 | .get("users/2");
27 | Assert.assertEquals(res.statusCode(),200);
28 | System.out.println(res.getBody().asString());
29 | }
30 |
31 | @Test
32 | public void userNotFound(){
33 | Response res = given().
34 | baseUri("https://reqres.in/api/").
35 | contentType("application/Json").
36 | when()
37 | .get("users/23");
38 | Assert.assertEquals(res.statusCode(),404);
39 | System.out.println(res.getBody().asString());
40 | }
41 |
42 | @Test
43 | public void createUser(){
44 | HashMap map = new HashMap();
45 | map.put("name" , "morpheus");
46 | map.put("job" , "leader");
47 | String user = "{\"name\" : \"morpheus\" , \"job\" : \"leader\"}";
48 | Response res = given()
49 | .baseUri("https://reqres.in/api/")
50 | .contentType("application/json")
51 | .body(user)
52 | .when()
53 | .post("users");
54 | Assert.assertEquals(res.statusCode(),201);
55 | System.out.println(res.getBody().asString());
56 | }
57 |
58 | @Test
59 | public void updateUser(){
60 | HashMap map = new HashMap();
61 | map.put("name" , "morpheus");
62 | map.put("job" , "leader");
63 | String user = "{\"name\" : \"morpheus\" , \"job\" : \"zion resident\"}";
64 | Response res = given()
65 | .baseUri("https://reqres.in/api/")
66 | .contentType("application/json")
67 | .body(user)
68 | .when()
69 | .put("users/2");
70 | Assert.assertEquals(res.statusCode(),200);
71 | System.out.println(res.getBody().asString());
72 | }
73 |
74 | @Test
75 | public void deleteUsers(){
76 | Response res = given().
77 | baseUri("https://reqres.in/api/users/2").
78 | contentType(ContentType.JSON).
79 | when()
80 | .delete();
81 | Assert.assertEquals(res.statusCode(),204);
82 | }
83 |
84 | @Test
85 | public void registerUser(){
86 |
87 | String user = "{\"email\" : \"eve.holt@reqres.in\" , \"password\" : \"pistol\"}";
88 | Response res = given()
89 | .baseUri("https://reqres.in/api/")
90 | .contentType("application/json")
91 | .body(user)
92 | .when()
93 | .post("register");
94 | Assert.assertEquals(res.statusCode(),200 , "Registration failed");
95 | System.out.println(res.getBody().asString());
96 | }
97 |
98 | @Test
99 | public void registerNotSucceseful(){
100 | String user = "{\"email\" : \"eve.holt@reqres.in\"}";
101 | Response res = given()
102 | .baseUri("https://reqres.in/api/")
103 | .contentType("application/json")
104 | .body(user)
105 | .when()
106 | .post("register");
107 | Assert.assertEquals(res.statusCode(),400 , "Registration failed because password missing!");
108 | System.out.println(res.getBody().asString());
109 | }
110 |
111 | @Test
112 | public void loginSucceseful(){
113 | String user = "{\"email\" : \"eve.holt@reqres.in\" , \"password\" : \"cityslicka\"}";
114 | Response res = given()
115 | .baseUri("https://reqres.in/api/")
116 | .contentType("application/json")
117 | .body(user)
118 | .when()
119 | .post("login");
120 | Assert.assertEquals(res.statusCode(),200 , "Login failed");
121 | System.out.println(res.getBody().asString());
122 | }
123 |
124 | @Test
125 | public void loginUnSucceseful(){
126 | String user = "{\"email\" : \"eve.holt@reqres.in\" }";
127 | Response res = given()
128 | .baseUri("https://reqres.in/api/")
129 | .contentType("application/json")
130 | .body(user)
131 | .when()
132 | .post("login");
133 | Assert.assertEquals(res.statusCode(),400 , "Login failed because Password missing!");
134 | System.out.println(res.getBody().asString());
135 | }
136 |
137 | @Test
138 | public void delayed(){
139 | String user = "{\"email\" : \"eve.holt@reqres.in\" }";
140 | Response res = given()
141 | .baseUri("https://reqres.in/api/users?delay=3")
142 | .contentType("application/json")
143 | .body(user)
144 | .when()
145 | .get();
146 | Assert.assertEquals(res.statusCode(),200);
147 | System.out.println(res.getBody().asString());
148 | }
149 | }
--------------------------------------------------------------------------------
/target/test-classes/requestRes.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alphabin-qa/demo-rest-assured-java/67ece376d5e7405792dba75479444b0be2f23f8d/target/test-classes/requestRes.class
--------------------------------------------------------------------------------