java – 如何将参数传递给Rest-Assured

有人可以在这种情况下帮助我:

当我调用这项服务时,http://restcountries.eu/rest/v1/,我得到了几个国家的信息.

但是当我想获得像芬兰这样的特定国家信息时,我会调用网络服务http://restcountries.eu/rest/v1/name/Finland来获取与国家相关的信息.

自动执行上述方案,如何在Rest-Assured中参数化国家/地区名称?我在下面试过,但没有帮助我.

RestAssured.given().
                    parameters("name","Finland").
            when().
                    get("http://restcountries.eu/rest/v1/").
            then().
                body("capital",containsstring("Helsinki"));

解决方法

正如文档所述:

REST Assured will automatically try to determine which parameter type
(i.e. query or form parameter) based on the HTTP method. In case of
GET query parameters will automatically be used and in case of POST
form parameters will be used.

但在你的情况下,似乎你需要路径参数而不是查询参数.
另请注意,获取国家/地区的通用URL是http://restcountries.eu/rest/v1/name/{country}
其中{country}是国家/地区名称.

然后,还有多种方式来传输路径参数.

这里有几个例子

使用pathparam()的示例:

// Here the key name 'country' must match the url parameter {country}
RestAssured.given()
        .pathParam("country","Finland")
        .when()
            .get("http://restcountries.eu/rest/v1/name/{country}")
        .then()
            .body("capital",containsstring("Helsinki"));

使用变量的示例:

String cty = "Finland";

// Here the name of the variable have no relation with the URL parameter {country}
RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/name/{country}",cty)
        .then()
            .body("capital",containsstring("Helsinki"));

现在,如果您需要调用不同的服务,您还可以像这样参数化“服务”:

// Search by name
String val = "Finland";
String svc = "name";

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}",svc,val)
        .then()
            .body("capital",containsstring("Helsinki"));


// Search by ISO code (alpha)
val = "CH"
svc = "alpha"

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}",containsstring("Bern"));

// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}",containsstring("Sofia"));

之后您还可以轻松使用JUnit @RunWith(Parameterized.class)为单元测试提供参数’svc’和’value’.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...