【发布时间】:2015-11-06 11:57:43
【问题描述】:
我想就如何为 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"
}
}
"""
第二种选择将(再次)使用cucumber,但删除技术细节,如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 |
第三个选项是使用 Spring,如 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代码,但可读性和跨团队交互不如黄瓜。
【问题讨论】:
标签: spring api rest testing cucumber