File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
src/test/java/demo/customassert Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments