import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.CustomComparator;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.Customization;
public class JsonComparisonTest {
@Test
void test_01() throws JSONException {
// language=json
String expect = "{ \"time\" : {\n" +
" \"insertTime\": \"201912010023\",\n" +
" \"updateTime\": \"202008111234\"\n" +
"},\n" +
"\"animal\": \"gorilla\"}\n";
// language=json
String actual = "{ \"time\" : {\n" +
" \"insertTime\": null,\n" +
" \"updateTime\": \"999999999999\"\n" +
"},\n" +
"\"animal\": \"gorilla\"}\n";
JSONAssert.assertEquals(expect, actual,
new CustomComparator(JSONCompareMode.STRICT,
new Customization("time.insertTime", (o1, o2) -> {
if (o2 == null) return true; // actualがnullの場合は無視
if (o1 instanceof String && o2 instanceof String) {
return ((String) o2).matches("201912010023"); // 正規表現で比較
}
return false;
}),
new Customization("time.updateTime", (o1, o2) -> {
if (o2 == null) return true; // actualがnullの場合は無視
if (o1 instanceof String && o2 instanceof String) {
return ((String) o2).matches(".*"); // 正規表現で比較
}
return false;
})
)
);
}
}