Jarree Arham Shahid
Jarree Arham Shahid

Reputation: 187

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.

Upvotes: 3

Views: 7171

Answers (3)

Sagor Chowdhuri
Sagor Chowdhuri

Reputation: 303

Firstly you need to fix the code and make sure about two imports.

import io.restassured.RestAssured;
import static org.hamcrest.Matchers.equalTo;

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

And make sure to add JARs.

Upvotes: 0

koos
koos

Reputation: 1

We use RestTest it's an easy free standalone app.

Add all your urls in one file and test conditions. One line One Test :)

Upvotes: 0

Mane
Mane

Reputation: 163

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;

Upvotes: 7

Related Questions