2015-08-28 30 views
2

我是cucumber新。我已經爲需要的罐子配置了cucumber的環境。我想用cucumber來測試其餘的api。所以首先創建了.feature文件並生成了基本的步驟定義。發送從黃瓜功能產生的步驟獲取請求

.feature文件:

Feature: Test 

    Scenario: List accounts 
    Given the system knows about the following details: 
     | name | value | 
     | unit | 01 | 
     | dept | 001 | 
    When the client requests accounts 
    Then the response code should be 200 
    And the response should contain following details: 
     | name | value | 
     | unit | 01  | 
     | dept | 001  | 
     | acctype | current | 

而且TestClass.java如下:

package testPackage; 

import org.junit.runner.RunWith; 

import cucumber.api.CucumberOptions; 
import cucumber.api.DataTable; 
import cucumber.api.PendingException; 
import cucumber.api.java.en.*; 
import cucumber.api.junit.Cucumber; 

@RunWith(Cucumber.class) 
@CucumberOptions(
     features={"classpath:cucumber.features/"}, 
     glue = {"testPackage"}  
     ) 

public class TestClass { 

    @Given("^the system knows about the following details:$") 
    public void the_system_knows_about_the_following_details(DataTable arg1) throws Throwable { 
     // Write code here that turns the phrase above into concrete actions 
     // For automatic transformation, change DataTable to one of 
     // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>. 
     // E,K,V must be a scalar (String, Integer, Date, enum etc) 
     throw new PendingException(); 
    } 

    @When("^the client requests accounts$") 
    public void the_client_requests_accounts() throws Throwable { 
     // Write code here that turns the phrase above into concrete actions 
     throw new PendingException(); 
    } 

    @Then("^the response code should be (\\d+)$") 
    public void the_response_code_should_be(int arg1) throws Throwable { 
     // Write code here that turns the phrase above into concrete actions 
     throw new PendingException(); 
    } 

    @Then("^the response should contain following details:$") 
    public void the_response_should_contain_following_details(DataTable arg1) throws Throwable { 
     // Write code here that turns the phrase above into concrete actions 
     // For automatic transformation, change DataTable to one of 
     // List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>. 
     // E,K,V must be a scalar (String, Integer, Date, enum etc) 
     throw new PendingException(); 
    } 

} 

我搜索了很多,但能不能找到從.java文件發送GET請求。

如何發送GET請求cucumber步驟定義和比較json響應?

+4

生成的步驟只是方法,所以你可以在它們中做任何其他的java方法。我認爲[此鏈接](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests)將幫助您製作HTTP請求。 –

回答

0

如果您願意使用Spring,則可以使用Spring's REST Template以RESTful方式發送HTTP請求。

Jackson也是很好的解析來自預定義的java數據對象的響應/請求,這使得處理JSON的痛苦顯着減輕。