2015-11-06 196 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)格式时增加了测试的可读性。 –