2015-11-06 194 views
0

我想獲得有關如何爲Rest API進行集成測試的不同觀點。Rest API的集成測試

第一種選擇將使用黃瓜「黃瓜書」如所描述的:使用黃瓜(再次)

Scenario: Get person 
    Given The system knows about the following person: 
    | fname | lname | address | zipcode | 
    | Luca | Brow | 1, Test | 098716 | 
    When the client requests GET /person/(\d+) 
    Then the response should be JSON: 
    """ 
    { 
     "fname": "Luca", 
     "lname": "Brow", 
     "address": { 
     "first": "1, Test", 
     "zipcode": "098716" 
     } 
    } 
    """ 

第二個方案是,但去除技術詳細描述here

Scenario: Get person 
    Given The system knows about the following person: 
    | fname | lname | address | zipcode | 
    | Luca | Brow | 1, Test | 098716 | 
    When the client requests the person 
    Then the response contains the following attributes: 
    | fname   | Luca | 
    | lname   | Brow | 
    | address :first | 1, Test | 
    | address :zipcode | 098716 | 

和Th Ë第三種選擇是使用描述here

private MockMvc mockMvc; 

@Test 
public void findAll() throws Exception { 
    mockMvc.perform(get("/person/1")) 
      .andExpect(status().isOk()) 
      .andExpect(content().mimeType(IntegrationTestUtil.APPLICATION_JSON_UTF8)) 
      .andExpect(jsonPath("$.fname", is("Luca"))) 
      .andExpect(jsonPath("$.lname", is("Brow"))) 
      .andExpect(jsonPath("$.address.first", is("1, Test"))) 
      .andExpect(jsonPath("$.address.zipcode", is("098716"))); 
} 

我真的很喜歡,因爲它看起來更清潔企業用戶和測試者的第二選擇,但另一方面也爲開發者將消耗該API的第一個選項看起來更可見,因爲它顯示了JSON響應。

第三個選項是最簡單的選擇,因爲它只是Java代碼,但可讀性和跨團隊交互不如黃瓜好。

回答

1

你應該使用第三個選項,但不使用junit,你應該使用spock.This是兩個世界中最好的。

斯波克測試是這樣寫的

def "description of what you want to test"() { 
    given: 
     //Do what is pre-requisite to the test 

    when: 
     def response = mockMvc.perform(get("/person/id")).andReturn().getResponse(); 

    then: 
     checkForResponse.each { 
     c->c(response) 

    } 

    where: 
     id  | checkResponse 
     1  | [ResponseChecker.correctPersondetails()] 
     100  | [ResponseChecker.incorrectPersondetails()] 

    } 
0

集成測試用於測試應用程序的組件是否可以一起工作。例如,您使用集成測試來測試對數據庫和mvc控制器的一些請求。集成測試在這裏測試您的基礎架構。

另一方面,BDD測試是爲了促進開發和規範之間的溝通。通常的想法是通過例子編寫測試或規範。絕對沒有設計來編寫集成測試。

我會推薦第三個選項。

+0

你BDD的定義不正確,BDD測試的行爲,或軟件的公共接口,在這種情況下是應用程序公開的端點。同樣,BDD在使用GWT(Given-When_then)格式時增加了測試的可讀性。 –