ドキュメントが説明したよう:アシュアード
RESTは、自動的にHTTPメソッドに基づいたパラメータタイプ (すなわち、クエリまたはフォームパラメータ)を決定しようとします。 の場合、GETクエリパラメータが自動的に使用され、POSTの場合は フォームパラメータが使用されます。
あなたのケースでは、代わりにパスパラメータが必要です。 また、国を取得するための一般的なURLはhttp://restcountries.eu/rest/v1/name/{country}
です。ここで、{country}
は国名です。
次に、パスパラメータを転送する方法は複数あります。変数を使用して
// 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"));
さて、あなたはまた、パラメータ化することができます。ここ
は、いくつかの例は、pathParam()を使用して
例ですこのような「サービス」:
// 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}", svc, val)
.then()
.body("capital", containsString("Bern"));
// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"
RestAssured.given()
.when()
.get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
.then()
.body("capital", containsString("Sofia"));
JUnit @RunWith(Parameterized.class)
を使って単体テストのパラメータ 'svc'と 'value'を簡単に入力することもできます。
私は、http://restcountries.eu/rest/v1?name=Finlandを呼び出すとur Webサービスの呼び出しが正しくないように見えますが、私はすべての国の情報を取得しています。 /restcountries.eu/rest/v1/name/Finland "特定の国情報を取得しています。 – Uday
私の答えはすでに修正されています。 pls check –
値でパラメータを渡すと、ここにパラメータを持つ利点は何ですか? \t文字列strCountryName = "フィンランド"; \t \t RestAssured.given()。 // \t \t \t \t \t \t \tパラメータ( "name"、 "Finland")。 \t \t \t \t \t()。 \t \t \t \t \t \t \t GET( "http://restcountries.eu/rest/v1/name/" + strCountryName)。 \t \t \t \t \t()。 \t \t \t \t \t \t body( "capital"、hasItem( "Helsinki")));なぜ私はコメントした上記のパラメータが必要ですか? – Uday