Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestApi testing using Rest-Assured

I am new to REST-Api testing. i am getting started with Rest-Assured for Rest-Api testing. i am having an issue in my first ever testcase.

The code is as follows:

public void testGetSingleUser() {
  expect().
    statusCode(200).
    body(
      "email", equals("[email protected]"),
      "firstName", equals("Tim"),
      "lastName", equals("Testerman"),
      "id", equals("1")).
    when().
    get("/service/single-user");
}

In this code the "expect()." command is not working. I need to fix this issue quickly and move on.

like image 545
Jarree Arham Shahid Avatar asked Jun 25 '26 19:06

Jarree Arham Shahid


1 Answers

Your request can't compile because you forget given() and you have to use equalTo() instead of equals().

Try this request:

given().
            expect().
            statusCode(200).
            body("email", equalTo("[email protected]")).
            body("firstName", equalTo("Tim")).
            body("lastName", equalTo("Testerman")).
            body("id", equalTo("1")).
        when().
            get("/service/single-user");

Also double check your imports:

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
like image 99
Mane Avatar answered Jun 28 '26 14:06

Mane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!