0

I need a test case that verifies that a '400 Bad Request' is returned when an API call is made without the required field. (Previously there was a bug where the field was needed, but requests without were being accepted)

It's a simple POST call with auth and a raw body. It returns a 200 when the missing field is returned.

The POST returns the '400 Bad Request' correctly, but I can't get the test to pass.

The following tests all fail:

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

The response body is:

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"])
kittenchops
  • 180
  • 1
  • 8

1 Answers1

1

There is a way where you can explicitly make some tests to Pass or Fail. Refer following code snippet this might help you.

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