Rest 的集成测试 API
Integration tests for Rest API
我想了解有关如何为 Rest 创建集成测试的不同观点 APIs。
第一个选项是使用 cucumber,如 "The Cucumber Book":
中所述
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 |
第三个选项将使用 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 代码,但可读性和跨团队交互不如黄瓜。
进行集成测试是为了测试您的应用程序的组件是否可以协同工作。例如,您使用集成测试测试对数据库和 mvc 控制器的一些请求。集成测试用于测试您的基础设施。
另一方面,BDD测试是为了促进开发和规范之间的沟通。常见的想法是通过示例编写测试或规范。肯定没有写集成测试的设计。
我会推荐第三个选项。
你应该使用第三个选项而不是 junit,你应该使用 spock.This 是两全其美。
Spock 测试是这样写的
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()]
}
我想了解有关如何为 Rest 创建集成测试的不同观点 APIs。
第一个选项是使用 cucumber,如 "The Cucumber Book":
中所述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 |
第三个选项将使用 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 代码,但可读性和跨团队交互不如黄瓜。
进行集成测试是为了测试您的应用程序的组件是否可以协同工作。例如,您使用集成测试测试对数据库和 mvc 控制器的一些请求。集成测试用于测试您的基础设施。
另一方面,BDD测试是为了促进开发和规范之间的沟通。常见的想法是通过示例编写测试或规范。肯定没有写集成测试的设计。
我会推荐第三个选项。
你应该使用第三个选项而不是 junit,你应该使用 spock.This 是两全其美。
Spock 测试是这样写的
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()]
}