2015-09-09 13 views
6

誰かがこのシナリオで私を助けることができる、安心する:私は、このサービス、http://restcountries.eu/rest/v1/を起動すると渡す方法のパラメータは

が、私は国情報のカップルを取得します。

フィンランドのような特定の国情報を取得する場合は、http://restcountries.eu/rest/v1/name/FinlandというWebサービスを呼び出して、国の関連情報を取得します。

上記のシナリオを自動化するには、国名をパラメータ化するにはどうすれば安心保証?私は以下を試しましたが、私を助けません。

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

答えて

0

GET呼び出しを間違って呼び出しています。それぞれ

parameters("name","Finland")のためだけクエリパラメータまたはフォームパラメータとして変換されますPOST/PUTをGET

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

行うための唯一の方法です。それはJavaのDSLですので、あなたは)すべて自分でURLを構築し、(得るためにそれを渡すことができ

を必要に応じて、GETリクエストとURLは次のようにして、同じ内容フェッチしなければならなかった場合:

http://restcountries.eu/rest/v1?name=Finland

あなたのDSLのようなものになります:あなたがGETリクエストを持っている場合は、あなたのパラメータはqueryParametersに変身

RestAssured.given().parameters("name","Finland"). 
       when(). 
         get("http://restcountries.eu/rest/v1") 

。このリンクから

さらに詳しい情報: https://code.google.com/p/rest-assured/wiki/Usage#Parameters

+0

私は、http://restcountries.eu/rest/v1?name=Finlandを呼び出すとur Webサービスの呼び出しが正しくないように見えますが、私はすべての国の情報を取得しています。 /restcountries.eu/rest/v1/name/Finland "特定の国情報を取得しています。 – Uday

+0

私の答えはすでに修正されています。 pls check –

+0

値でパラメータを渡すと、ここにパラメータを持つ利点は何ですか? \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

9

ドキュメントが説明したよう:アシュアード

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'を簡単に入力することもできます。

+1

これはパスの値をパラメータ化するような洗練された方法です! RequestSpecification APIで述べたように、このアプローチは実際に可読性を向上させ、パスの再利用を可能にします。 http://static.javadoc.io/com.jayway.restassured/rest-assured/1.2.3/com/jayway/restassured/specification/RequestSpecification.html#pathParameter(java.lang.String,%20java.lang)。オブジェクト) –

関連する問題