微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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’.

原文地址:https://www.jb51.cc/java/127744.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐