I am a beginner in junit I would like to test two JsonObject with dynamic data especially the date.
I have a proprety "CreationDate", In the first json I use the localDateTime "now()" on the other hand the jsonTest file contains a literal date.
JsonObject actual = (JsonObject) parser.parse(myListResponse.readEntity(String.class));
JsonObject expected = (JsonObject) parser.parse(fixture("path/jsonTest.json"));
assertThat(actual , is(expected));
How I can resolve this to get a valid test
You have to create a custom Matcher by extending BaseMatcher. Then you can do the comparison/matching by using the desired fields. For example:
static class JsonObjectMatcher extends BaseMatcher<JsonObject> {
private final JsonObject target;
public static JsonObjectMatcher customMatchesTheJson(final JsonObject expected) {
return new JsonObjectMatcher(expected);
}
private JsonObjectMatcher(JsonObject target) {
this.target = target;
}
@Override
public boolean matches(Object o) {
if (o instanceof JsonObject) {
final JsonObject other = (JsonObject) o;
// TODO: 5/11/21 Custom compare target to other
}
return false;
}
@Override
public void describeTo(Description description) {
// TODO: 5/11/21 Add to the description
}
}
And you can use it to match as follows:
assertThat(actual, JsonObjectMatcher.customMatchesTheJson(expected));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.