Skip to content

Commit c34334b

Browse files
AssertJ API Test
1 parent 0d6cba7 commit c34334b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package demo.customassert;
2+
3+
import io.restassured.response.Response;
4+
import org.assertj.core.api.AbstractAssert;
5+
import org.assertj.core.api.SoftAssertions;
6+
7+
import java.util.function.Predicate;
8+
9+
public class APIAssert extends AbstractAssert<APIAssert, Response> {
10+
11+
SoftAssertions assertions;
12+
private APIAssert(Response response) {
13+
super(response, APIAssert.class);
14+
assertions=new SoftAssertions();
15+
}
16+
17+
public static APIAssert assertThat(Response response){
18+
return new APIAssert(response);
19+
}
20+
21+
public APIAssert statusCodeIs(int statusCode){
22+
assertions.assertThat(actual.statusCode()).isEqualTo(statusCode);
23+
return this;
24+
}
25+
26+
public APIAssert hasKeyWithValue(Predicate<Response> predicate){
27+
assertions.assertThat(predicate).accepts(actual);
28+
return this;
29+
}
30+
31+
public void assertAll(){
32+
assertions.assertAll();
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package demo.customassert;
2+
3+
import io.restassured.RestAssured;
4+
import io.restassured.response.Response;
5+
import org.testng.annotations.Test;
6+
7+
import java.util.function.Predicate;
8+
9+
import static demo.customassert.APIAssert.assertThat;
10+
11+
public class APITest {
12+
13+
@Test
14+
public void apiAssertTest(){
15+
Response response=RestAssured.given().get("https://reqres.in/api/users/2");
16+
Predicate<Response> keyValue=res -> res.jsonPath().getInt("data.id")==2;
17+
assertThat(response)
18+
.statusCodeIs(200)
19+
.hasKeyWithValue(keyValue)
20+
.assertAll();
21+
}
22+
}

0 commit comments

Comments
 (0)