2015-09-09 6 views
6

कोई मुझे इस परिदृश्य में मदद कर सकते हैं:कैसे पारित करने के लिए मानकों को आराम करने के लिए बीमित

जब मैं इस सेवा, http://restcountries.eu/rest/v1/ आह्वान, मैं देशों जानकारी की जोड़ी मिलता है।

लेकिन जब मैं फिनलैंड कहने की तरह किसी विशिष्ट देश की जानकारी प्राप्त करना चाहता हूं, तो मैं देश से संबंधित जानकारी प्राप्त करने के लिए वेब सेवा को http://restcountries.eu/rest/v1/name/Finland के रूप में आमंत्रित करता हूं।

उपर्युक्त परिदृश्य को स्वचालित करने के लिए, मैं में देश के नाम को पैरामीटर कैसे कर सकता हूं पुन: आश्वासन? मैंने नीचे कोशिश की, लेकिन मेरी मदद नहीं करता है।

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

उत्तर

0

आप गलत तरीके से प्राप्त कॉल लागू कर रहे हैं।

parameters("name","Finland")प्रश्न के रूप में केवल परिवर्तित किया जाएगा पैरामीटर या रूप के लिए पैरामीटर प्राप्त और पोस्ट/डाल क्रमशः

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

करने के लिए एक ही रास्ता है। ,

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

: चूंकि यह एक जावा डीएसएल है, तो आप सब अपने आप से यूआरएल का निर्माण और इसे पाने के लिए) पास (यदि

आवश्यक कर सकते हैं GET अनुरोध के साथ URL एक ही विवरण की तरह किया गया लाने के लिए किया था

अपने डीएसएल की तरह कुछ होगा: आप एक GET अनुरोध है जब

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

आपके पैरामीटर queryParameters में बदलना। इस लिंक से

और जानकारी: https://code.google.com/p/rest-assured/wiki/Usage#Parameters

+0

ऐसा लगता है, जब मैं आमंत्रित करता हूं, तो आपकी वेब सेवा कॉल सही नहीं है, http://restcountries.eu/rest/v1?name=Finland, मुझे सभी देशों की जानकारी मिल रही है, बल्कि यदि आप "http://restcountries.eu/rest/v1/name/Finland "आपको विशिष्ट देश की जानकारी मिल रही है। – Uday

+0

मेरा उत्तर पहले ही ठीक किया गया है। कृपया –

+0

जांचें यदि हम मूल्य से पैरामीटर पास करते हैं, तो यहां पैरामीटर रखने का क्या फायदा है? \t स्ट्रिंग strCountryName = "फिनलैंड"; \t \t RestAssured.given()। // \t \t \t \t \t \t पैरामीटर ("नाम", "फिनलैंड")। \t \t \t \t \t जब()। \t \t \t \t \t \t \t प्राप्त ("http://restcountries.eu/rest/v1/name/" + strCountryName)। \t \t \t \t \t तब()। \t \t \t \t \t \t शरीर ("पूंजी", हैइटम ("हेलसिंकी")); मुझे ऊपर पैरामीटर की आवश्यकता क्यों है जिस पर मैंने टिप्पणी की? – Uday

9

प्रलेखन के रूप में बताते हैं:

इतना तय है कि स्वचालित रूप से निर्धारित करने की कोशिश करेंगे जो पैरामीटर प्रकार (यानी क्वेरी या प्रपत्र पैरामीटर) HTTP पद्धति पर आधारित । के मामले में क्वेरी पैरामीटर स्वचालित रूप से उपयोग किए जाएंगे और POST फ़ॉर्म पैरामीटर का उपयोग किया जाएगा।

लेकिन आपके मामले में ऐसा लगता है कि आपको क्वेरी पैरामीटर के बजाय पथ पैरामीटर की आवश्यकता है। ध्यान दें कि देश प्राप्त करने के लिए सामान्य यूआरएल 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")); 

अब अगर आप विभिन्न सेवाओं कॉल करने की आवश्यकता है, तो आप भी कर सकते हैं parametrize "सेवा" इस तरह:

// 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")); 

एक इकाई परीक्षण के लिए पैरामीटर 'svc' और 'value' को फ़ीड करने के लिए आप आसानी से JUnit @RunWith(Parameterized.class) का उपयोग भी कर सकते हैं।

+1

पथ मूल्यों को पैरामीटर करने का यह एक शानदार तरीका है! बस के रूप में RequestSpecification एपीआई ने कहा, इस दृष्टिकोण वास्तव में पठनीयता में सुधार और पथ पुन: उपयोग की अनुमति देता है है! http://static.javadoc.io/com.jayway.restassured/rest-assured/1.2.3/com/jayway/restassured/specification/RequestSpecification.html#pathParameter(java.lang.String,%20java.lang। वस्तु) –

संबंधित मुद्दे