需要帮助REST ASSURED

时间:2014-11-17 11:36:42

标签: rest rest-assured

我开始使用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());
}

方式-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 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

对于代码2中的异常,如果您在响应中嵌套了通用参数,则很难在不查看RESPONSE的情况下提及,但请尝试按照以下链接进行操作。

How to validate nested response using REST Assured?

如果您有任何问题或疑问,请与我们联系。谢谢!

答案 2 :(得分:1)

尽管这个问题很老,但我只是弄清了第二个问题:

  

线程中的异常" main" 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)values:[com.jayway.restassured.internal.RestAssuredResponseImpl@753455ab,...]可能的解决方案:wait(),any(),grep()

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

答案 3 :(得分:1)

谢谢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>

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

添加等于以下内容的静态包:

import static org.hamcrest.Matchers.*;
相关问题