I have the following validation where I have to check if returned body has a string containing "id": 6354
, but it interprets slashes of special characters. How I can validate strings which contain double quotation marks ?
Code
import static org.hamcrest.Matchers.containsString;
import com.jayway.restassured.response.Response;
response.then()
.body(containsString("\"id\": 6354"));
Error
Response body doesn't match expectation.
Expected: a string containing "\"id\": 6354"
Actual: {...,"id": 6354, ...}
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.
Class StringContains. Tests if the argument is a string that contains a substring. Creates a matcher that matches if the examined String contains the specified String anywhere.
Explanation: The anyOf Hamcrest matcher is just like the || operator.
Hamcrest containsString seems to print the escaped characters in the output error message, however it seems to correctly escape them when doing the matching.
In my example, I was incorrectly adding a space, so following the example in the question:
"id": 6354
would give the error Expected: a string containing "\"id\": 6354"
however when I changed it to "id":6354"
, it passed the assertion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With