Quick revision
The short answer
Comparing JSON as strings is fragile, so we compare it as JSON. JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT) ignores field order and extra fields, and a CustomComparator can ignore specific paths such as timestamp; JsonUnit does the same in AssertJ style with assertThatJson(actual).whenIgnoringPaths("timestamp").isEqualTo(expected). In Spring tests, MockMvc's content().json() and jsonPath() matchers and Spring Boot's JacksonTester and JsonContent cover the same need, and the expected JSON is best kept in files under src/test/resources.
What to remember
In this answer, we saw why comparing JSON as strings fails on field order, whitespace and volatile fields like timestamps. JSONAssert's LENIENT mode ignores order and extra fields, and a CustomComparator relaxes only chosen paths; JsonUnit gives the same comparison in AssertJ style with whenIgnoringPaths and options for array order; and Spring's MockMvc json() and jsonPath() matchers and Spring Boot's JacksonTester cover the same needs inside Spring tests. Expected documents belong in files under src/test/resources.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Comparing JSON as strings is fragile, so we compare it as JSON. JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT) ignores field order and extra fields, and a CustomComparator can ignore specific paths such as timestamp; JsonUnit does the same in AssertJ style with assertThatJson(actual).whenIgnoringPaths("timestamp").isEqualTo(expected). In Spring tests, MockMvc's content().json() and jsonPath() matchers and Spring Boot's JacksonTester and JsonContent cover the same need, and the expected JSON is best kept in files under src/test/resources.
Why this question is asked
Almost every REST service has tests that check a JSON response, and almost every team has at some point written one that compares strings and breaks when someone adds a field or Jackson changes the order of properties. Interviewers ask this to see whether we know that JSON has its own equality rules, and which libraries apply them. In this answer, we will see why string comparison fails, write the test with JSONAssert, then with JsonUnit, look at the tools Spring and Spring Boot give us, and finish with how to keep expected documents readable.
1Why Comparing Strings Fails
JSON objects are unordered collections of fields. {"id":42,"status":"PAID"} and {"status":"PAID","id":42} mean exactly the same, but as strings they differ. Whitespace and line breaks differ too depending on whether the server pretty-prints. And our response has a timestamp that changes on every call.
Notice that the data the test cares about is identical. The test fails for reasons that have nothing to do with correctness, which is exactly what makes such tests annoying and eventually ignored.
2JSONAssert: Lenient Comparison and Ignored Paths
JSONAssert parses both strings and compares them as JSON. Its compare mode decides how strict it is. LENIENT allows extra fields in the actual document and ignores array order; STRICT allows neither. Field order inside objects never matters in any mode, because that is how JSON works.
| Approach | Field order | Extra fields | Array order |
|---|---|---|---|
String assertEquals | no | no | no |
| JSONAssert LENIENT | yes | yes, any | yes |
| JSONAssert STRICT + Customization | yes | only the customized path | no |
JsonUnit + whenIgnoringPaths | yes | only ignored paths | no, unless an option is set |
LENIENT is often enough, but it has a downside: it also accepts any other extra field, and array elements in any order. When we want the response checked strictly and only the timestamp relaxed, a CustomComparator with STRICT mode and a Customization for the timestamp path does that. Notice that in strict mode the field must exist in the expected document, so we give it a placeholder value, and the customization accepts any actual value for it.
3JsonUnit: the Same Idea in AssertJ Style
JsonUnit offers JSON comparison as AssertJ assertions, which reads well next to the other assertions in a test and gives clear difference messages. By default it ignores field order and is strict about everything else, and we relax it explicitly.
If the status were wrong, the failure would name the path, something like 'Different value found in node "status", expected: <"PAID"> but was: <"OPEN">', which is much faster to read than a diff of two long strings. JsonUnit also supports placeholders such as "${json-unit.any-string}" inside the expected document, an alternative to ignoring a path.
4Using Spring's Own JSON Assertions
In a Spring MVC test with MockMvc we often do not need another library. content().json(expected) compares the body with JSONAssert, leniently by default, and jsonPath() checks single fields with JsonPath expressions. Newer Spring versions let us pass a compare mode for strict checks (check your version for the exact parameter).
Spring Boot's JacksonTester returns a JsonContent, which has AssertJ-style assertions such as isEqualToJson and the extractingJsonPath methods. @JsonTest starts only the JSON-related part of the context, so these tests are fast. They test our serialization, while MockMvc tests check the whole request handling.
5Keeping Expected JSON in Files
A response with twenty fields written as a Java string is hard to read and review. Putting the expected document in a file under src/test/resources, for example src/test/resources/orders/order-42.json, keeps it formatted, lets reviewers see changes as clean diffs, and lets several tests share it.
6Follow-up questions
Why not deserialize the JSON into objects and compare them?
That works for checking values and is fine in many tests, but it tests a different thing. Deserializing hides unexpected extra fields, renamed properties that happen to map, and formatting problems such as a date serialized in the wrong format. When the JSON contract itself is what we test, comparing JSON is more faithful.
When should a test be strict about extra fields?
When the response is a public contract and adding a field by accident, such as an internal id, would be a leak or a breaking change for strict clients. Then STRICT mode with explicit exceptions, or JsonUnit's default strictness, catches it. For internal APIs, lenient comparison keeps tests from breaking on every harmless addition.
7Conclusion
In this answer, we saw why comparing JSON as strings fails on field order, whitespace and volatile fields like timestamps. JSONAssert's LENIENT mode ignores order and extra fields, and a CustomComparator relaxes only chosen paths; JsonUnit gives the same comparison in AssertJ style with whenIgnoringPaths and options for array order; and Spring's MockMvc json() and jsonPath() matchers and Spring Boot's JacksonTester cover the same needs inside Spring tests. Expected documents belong in files under src/test/resources.
Open with
I would never compare the JSON as strings, because field order and whitespace can change without any real difference, and the timestamp changes on every call. Instead I compare it as JSON.
Then, if they want more
With JSONAssert, assertEquals with JSONCompareMode.LENIENT ignores field order and allows extra fields like the timestamp, and it also ignores array order. If I want everything else strict, I use a CustomComparator in STRICT mode with a Customization for the timestamp path that accepts any value, with a placeholder in the expected document.
With JsonUnit, which I like because it is AssertJ style, I write assertThatJson(actual).whenIgnoringPaths("timestamp").isEqualTo(expected). It ignores field order by default and is strict about the rest, and Option.IGNORING_ARRAY_ORDER relaxes arrays if needed. Its failure messages name the path that differs.
Inside Spring tests, MockMvc's content().json() does a lenient JSONAssert comparison and jsonPath() checks single fields, and in Spring Boot a @JsonTest with JacksonTester lets me assert isEqualToJson against a file to test our serialization. Finally, I keep the expected JSON in files under src/test/resources, so it stays readable and reviewable.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a test that checks a JSON response body from a REST call matches an expected...": https://scrivio-interview-library.netlify.app/java/junit/4d96102983/