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 种方法,第一种方法有问题,第二种方法运行良好。请让我知道我在第一种方法中缺少什么。

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

方法二

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 就可以工作。我不知道为什么 Rest Assured 网站没有注意到这一点。在 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

对于 Code-1,对于equalTo()您必须导入的方法org.hamcrest.Matchers.*;

对于代码 2 中的异常,如果不查看 RESPONSE 就很难提及,但如果您的响应中有嵌套的通用参数,请尝试遵循以下链接。

如何使用 REST Assured 验证嵌套响应?

如果您有任何问题或疑问,请告诉我。谢谢!

于 2015-04-12T06:16:02.290 回答
1

将第一个示例的正文更改为:

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

这是因为来自服务器的 JSON 响应不是一个对象,而是一个数组,您必须查询第一个对象 ( [0]),然后是名称 ( .name)。

于 2014-11-21T21:14:04.113 回答
1

尽管这个问题很老,但我只是偶然发现了第二个问题:

线程“主”groovy.lang.MissingMethodException 中的异常:没有方法签名:com.jayway.restassured.internal.ContentParser.parse() 适用于参数类型:(com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway .restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) 值:[com.jayway.restassured.internal.RestAssuredResponseImpl@753455ab, ...] 可能的解决方案:wait(), any (), grep()

这是由于缺少依赖项。在我的情况下,我需要添加 xml-path 和 groovy-xml 的依赖项,即使我只是在使用 JSON 数据。所以最好的办法是传递地解决依赖关系。

于 2016-10-27T06:14:22.840 回答
1

equalTo来自 Hamcrest,它是包含在 JUnit jar 中的 JUnit 依赖项。您可能只需要从 Hamcrest 为它导入静态方法。

import static org.hamcrest.core.IsEqual.*;
于 2019-03-05T16:08:07.790 回答
0

添加一个静态包等于:

import static org.hamcrest.Matchers.*;
于 2020-04-30T20:16:13.673 回答