I am API testing and I've noticed there aren't any negative matchers in the RestAssuredMatchers library. What I mean is:
For example:
Code:
Response response = expect().body("status",equalTo("success")).given()
.contentType("application/json").with()
.body(request).when().post(server);
Here I am basically saying with this "request" body, expect that status key in the response to equal "success".
From here I can extract status code to ensure this was a successful response form server and make a positive assertion
Code:
int statusCode = response.getStatusCode();
Assert.assertTrue(statusCode == 200, "status code should have been 200");
So here is where my question is:
Is there a library or some way I can do negative assertions and/or expectations such as:
Code:
do().not().expect().body("status",equalTo("success")).given()
.contentType("application/json").with()
.body(request).when().post(server);
int statusCode = response.getStatusCode();
or RestAssured.assertFalse(statusCode == 404);
etc...
Can anyone please help?
So figured out the parameter to RestAssured posts is not json dataformat but json payloads converted to strings. So you can do a negative assertion by converting to string then doing a contains...and then wrap that around an assertion. Here's a code example:
Code:
Response responseLiheap = RestAssured.given().contentType("application/json").with()
.body(requestString).when().post(server);
Assert.assertFalse(responseLiheap.asString().contains("Whatever_it_should_not_contain"));
If you wanna inspect the response yourself you can print out the response by capturing the data the server sends back by this:
Code:
ResponseBody<?> body = responseLiheap.getBody();
logger.info("Response:--->" + body.asString());
You can do a sysout or like me, use a logger and just read from that data as a sanity check for yourself.