0

以下のコードの実行中にエラーが発生して、REST Assured から始めています。

コード 1-

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

例外-

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

コード 2 -

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

例外-

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()

以下は、私のクラスで唯一の 2 つのメソッドです。最初のメソッドに問題があり、2 つ目のメソッドは正常に動作しています。最初の方法で何が欠けているか教えてください。

方法-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());
}

方法-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"));
}
4

6 に答える 6

2

Htiに感謝します。あなたの答えはうまくいきました。他の依存関係がなければ、安心して動作します。Rest Assured の Web サイトがこれに注意しない理由がわかりません。pom.xmlのフォローはうまくいきました

<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>
于 2017-05-03T23:53:24.853 に答える
1

最初の例の本文を次のように変更します。

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

これは、サーバーからの JSON 応答がオブジェクトではなく配列[0]であり、最初のオブジェクト ( )、次に名前 ( )を照会する必要があるため.nameです。

于 2014-11-21T21:14:04.113 に答える