Java ';大多数';RESTful API上小黄瓜的正确格式
我目前正在编写一个JavaAPI,它有一个端点,我希望能够在该端点上执行POST、PATCH、GET和DELETE操作 下面是一篇文章的JSON示例Java ';大多数';RESTful API上小黄瓜的正确格式,java,junit,gherkin,Java,Junit,Gherkin,我目前正在编写一个JavaAPI,它有一个端点,我希望能够在该端点上执行POST、PATCH、GET和DELETE操作 下面是一篇文章的JSON示例 { "type": "suppliers", "name":"Toilet Duck Network Energy LTD" } 下面是API的响应,我正在对其进行格式化,以符合: 我已经启动并运行了端点,我已经完成了我的小黄瓜故事和步骤定义。这是小黄瓜的样品 Background: Given the system kn
{
"type": "suppliers",
"name":"Toilet Duck Network Energy LTD"
}
下面是API的响应,我正在对其进行格式化,以符合:
我已经启动并运行了端点,我已经完成了我的小黄瓜故事和步骤定义。这是小黄瓜的样品
Background:
Given the system knows about the following Suppliers:
| supplierid | suppliername |
| 1 | Blue Network Energy LTD |
| 2 | Red Network Energy LTD |
| 3 | Orange Network Energy LTD |
| 4 | Green Network Energy LTD |
| 5 | Yellow Network Energy LTD |
| 6 | Violet Network Energy LTD |
Scenario Outline: SUPPLIER: GET RESOURCE - SUCCESS
And the client executes a GET request for <supplierid>
Then the response status is: <status>
And the response has the jsonapi version
And the response has the Supplier: <supplier>
Examples:
|supplierid | supplier | status |
| 1 | "Blue Network Energy LTD" | "SUCCESS" |
| 2 | "Red Network Energy LTD" | "SUCCESS" |
| 3 | "Orange Network Energy LTD" | "SUCCESS" |
| 4 | "Green Network Energy LTD" | "SUCCESS" |
| 5 | "Yellow Network Energy LTD" | "SUCCESS" |
| 6 | "Violet Network Energy LTD" | "SUCCESS" |
我一直想格式化小黄瓜,以允许更多的重复使用,但我发现还有其他编写小黄瓜的方法,这意味着我需要将内容某些部分的测试转移到其他地方
建议我这样做:
Given the system knows about the following suppliers:
| supplier |
| "Blue Network Energy LTD" |
| "Red Network Energy LTD" |
When the client requests a list of suppliers
Then the response is a list containing "2" suppliers
And one supplier has the attributes:
| id | supplier | type |
| 2 | "Red Network Energy LTD" | suppliers |
And one supplier has the attributes:
| id | supplier | type |
| 2 | "Red Network Energy LTD" | suppliers |
但这并没有留下任何检查以下内容的空间:
- 内容类型标题
- HTTP状态代码
- 还有其他我还没有遇到的事情
是否应使用单元测试而不是功能文件来检查用户不感兴趣的这些项目?我建议使用另一种方法,看看它是否对您有用:
和
将响应验证为“Id=2,供应商=红色网络能源有限公司,类型=供应商,内容类型=文本/html,状态代码=404,状态=成功”
公共类定义{
@当“^validate response as\”([^\“]*)\“$”时;
公共void validateResponse(字符串输入){
String[]inputs=input.split(“,”);
HashMap字段=新的HashMap();
用于(字符串ip:输入){
字段.put(ip.split(“=”[0],ip.split(“=”[1]);
}
for(字符串字段:fields.keySet()){
字符串fieldValue=fields.get(字段);
//这里有字段名和字段值
开关(field.trim().toUpperCase()){
案例“ID”:
//您的代码在此验证id
打破
案例“供应商”:
//您的代码在此验证id
打破
//这里还有其他案例
}
}
}
@Then("^the response status is: \"([^\"]*)\"$")
public void the_response_status_is(String arg1) throws Throwable {
String collectionResponseStatus = "";
String resourceResponseStatus = "";
if (this.resourceResponseTest != null){
resourceResponseStatus = this.resourceResponseTest.getStatus().toString();
Assert.assertTrue(resourceResponseStatus.equals(arg1.toString()));
}
if (this.collectionResponseTest != null){
collectionResponseStatus = this.collectionResponseTest.getStatus().toString();
Assert.assertTrue(collectionResponseStatus.equals(arg1));
}
}
Given the system knows about the following suppliers:
| supplier |
| "Blue Network Energy LTD" |
| "Red Network Energy LTD" |
When the client requests a list of suppliers
Then the response is a list containing "2" suppliers
And one supplier has the attributes:
| id | supplier | type |
| 2 | "Red Network Energy LTD" | suppliers |
And one supplier has the attributes:
| id | supplier | type |
| 2 | "Red Network Energy LTD" | suppliers |
public class StepDefinitions{
@When("^validate response as \"([^\"]*)\"$");
public void validateResponse(String input){
String[] inputs = input.split(",");
HashMap<String, String> fields = new HashMap<String,String>();
for(String ip : inputs){
fields.put(ip.split("=")[0],ip.split("=")[1]);
}
for(String field : fields.keySet()){
String fieldValue = fields.get(field);
//You have both field name and field value here
switch(field.trim().toUpperCase()){
case "ID":
//your code here to validate id
break;
case "SUPPLIER":
//your code here to validate id
break;
//Other cases here
}
}
}