Need help in REST ASSURED

9.9k Views Asked by At

I am starting with REST Assured, getting error while executing below code :

Code 1-

 RestAssured.expect().statusCode(200).
        body(
              "name", equalTo("Russia")
                ).
            when().
                get("http://restcountries.eu/rest/v1/callingcode/7");

Exception-

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method equalTo(String) is undefined for the type

Code 2 -

    RestAssured.expect().statusCode(200).
        body(
              "name", Matchers.equalTo("Russia")
                ).
            when().
                get("http://restcountries.eu/rest/v1/callingcode/7");

Exception-

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl@753455ab, ...] Possible solutions: wait(), any(), grep()

Below are the only 2 methods in my class, I am having issue with first one, second one is running fine. Please let me know what I am missing in first method.

Method -1

public static void testCountriesCallingCode() {     
    RestAssured.expect().statusCode(200).
        body(
              "name", equalTo("Russia")
                ).
            when().
                get("http://restcountries.eu/rest/v1/callingcode/7");
    System.out.println(RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7").asString());
}

Method-2

public static void testCountriesCallingCodeUsingJSONPATH(){
    Response res = RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7");
    System.out.println(res.getStatusCode());
    String json = res.asString();
    JsonPath jp = new JsonPath(json);
    System.out.println(jp.get("name"));
}
6

There are 6 best solutions below

3
On

Change the body of your first example to:

body(
      "[0].name", equalTo("Russia")
    )

That is because the JSON response from the server is not an object, but an array, and you have to query for the first object ([0]), then the name (.name).

0
On

equalTo comes from Hamcrest which is a JUnit dependency contained within the JUnit jar. You probably just need to import the static method for it from Hamcrest.

import static org.hamcrest.core.IsEqual.*;
0
On

Add a static package for equal to:

import static org.hamcrest.Matchers.*;
0
On

Even though this question is old, I just stumpled upon the second problem:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl@753455ab, ...] Possible solutions: wait(), any(), grep()

This is due to missing dependencies. In my case I needed to add the dependencies for xml-path and groovy-xml, even though I'm just working with JSON data. So the best thing to do is resolving the dependencies transitively.

0
On

For the Code-1, for equalTo() method you have to import org.hamcrest.Matchers.*;

For the exception in code 2, it is very hard to mention without looking at the RESPONSE but try to follow below link if you have nested generic parameters in your response.

How to validate nested response using REST Assured?

Please let me know if you have any issue or question. Thanks!

0
On

Thanks Hti, your answer worked. Without the other dependencies, Rest Assured kind of works. I have no idea why Rest Assured website does not note this. Following in pom.xml worked

<properties>
    <rest-assured.version>3.0.2</rest-assured.version>
    <resteasy.version>3.0.17.Final</resteasy.version>
</properties>
...
<!-- Jackson is for allowing you to convert pojo (plain old Java object) into JSON -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>${rest-assured.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-path</artifactId>
    <version>${rest-assured.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>xml-path</artifactId>
    <version>${rest-assured.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-xml</artifactId>
    <version>2.4.11</version>
    <scope>test</scope>
</dependency>