0

我需要一个测试用例来验证在没有必填字段的情况下进行 API 调用时是否返回了“400 Bad Request”。(以前有一个需要该字段的错误,但没有的请求被接受)

这是一个带有身份验证和原始正文的简单 POST 调用。返回缺少的字段时返回 200。

POST 正确返回“400 Bad Request”,但我无法通过测试。

以下测试均失败:

pm.test("Status is an error", function () {
    pm.response.to.be.error;
});
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

响应正文是:

Instantiation of [simple type, class com.[company].[product].notifications.api.v2.models.NotificationCreateV2] value failed for JSON property content due to missing (therefore NULL) value for creator parameter content which is a non-nullable type
 at [Source: (byte[])"{
  "field1": "string",
  "field2": "Snort",
  "field3": "Signature 5102",
  "field4": "2019-04-19T10:34:03Z",
  "field5": 0,
  "field6": 4
}"; line: 8, column: 1] (through reference chain: com.[company].[product].notifications.api.v2.models.NotificationCreateV2["content"])
4

1 回答 1

1

有一种方法可以让您明确地进行一些测试以通过或失败。请参阅以下代码片段,这可能会对您有所帮助。

pass=true;
fail=false;
try{
    if(responseCode.code === 200)
    {
        var jsonData = pm.response.json();
        tests["Request with 200 status ok : "+ responseCode.code] = responseCode.code === 200 === fail;

    }else if(responseCode.code !== 200){
        console.error("Web-service failed");
        tests["Request with 400 Bad Request: "+ responseCode.code] = responseCode.code === 400;
    }
}catch(err){
    console.log("Something went wrong please contact to your Admin...!"+err);
}
于 2019-07-18T06:28:25.780 回答