'最'在RESTful API上使用正确的小黄瓜格式

时间:2017-07-24 09:39:12

标签: java junit gherkin

我目前正在编写一个java API,它有一个端点,我希望能够执行POST,PATCH,GET和DELETE操作。

这里是POST的样本JSON

{
    "type": "suppliers",
    "name":"Toilet Duck Network Energy LTD"
}

以下是来自API的回复,我将其格式化为与JSON-API format对应:

{
    "status": "SUCCESS",
    "jsonapi": {
        "version": "1.0"
    },
    "data": {
        "type": "suppliers",
        "name": "Toilet Duck Network Energy LTD"
    }
}

我已经启动并运行了端点,并且完成了我的小黄瓜故事和步骤定义。这是小黄瓜的一个样本

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" |

这里是Then the response status is: <status>的步骤定义。请注意,它允许计算POST,PATCH,GET(集合或资源)或DELETE的响应状态。很多可重用性。

@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));
    }

}

我一直想要对小黄瓜进行格式化以允许更多的重复使用,但我发现还有其他方式来编写小黄瓜,这意味着我需要在其他地方移动测试内容的某些部分

This article建议我做这样的事情:

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 |

但是这并没有留下任何检查以下内容的空间:

  • content-Type Header
  • HTTP状态代码
  • 和我还没有遇到的其他事情

是否应该使用单元测试而不是功能文件来检查用户不感兴趣的这些项目?

1 个答案:

答案 0 :(得分:0)

我会建议一种不同的方法,看看它对你有用:

And验证回复为“Id = 2,供应商= Red Network Energy LTD,类型=供应商,内容类型= text / html,状态代码= 404,状态=成功”

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
  }
 }
}