Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Flaky test of JsonSimpleStepTest #1886

Conversation

deekshacheruku
Copy link

Description
Fixes the flaky tests handlesComplexNestedObject, handlesNestedObject, handlesObjectWithNull inside JsonSimpleStepTest class

Root Cause
The tests has been reported as flaky when ran against the NonDex tool. The tests failed because it was trying to compare the two Json strings, where the JSON library internally uses HashMap. The HashMap in Java does not store the inserted order of keys and their values. As a result, when the expected Json string (hard-coded) is compared with the actual one, it failed.

Fix
The assertJsonEquals method from the JsonUnit library is used to verify that two JSON entities are structurally and value-wise equivalent in unit tests, ignoring the order of elements in objects. If the actual JSON does not match the expected JSON, the method throws an AssertionError.

Dependencies
The fix adds two Java dependencies: JsonUnit for flexible JSON assertions in tests, ensuring structural and value equivalence regardless of element order, and Jackson Databind for efficient parsing and generation of JSON data to and from Java objects. JsonUnit's assertJsonEquals method is specifically used to fix flaky tests by comparing JSON objects without considering the order of elements.

Java: openjdk version "11.0.20.1"

Module build - Successful
Command used -
./gradlew build -x test

Regular test - Successful
Command used -
./gradlew --info testlib:test --tests=<test above>

NonDex test - Failed
Command used -
./gradlew --info testlib:nondexTest --tests=<test above>

NonDex test passed after the fix.

I am open to any other methods of fixing the issues.
=> I've experimented with ObjectMapper and JsonNode, which require additional dependencies and necessitate handling JsonProcessingException, significantly increasing the codebase due to the added exception management.

@nedtwigg
Copy link
Member

Thanks for the PR!

  • needs spotlessApply
  • fixing a flaky test doesn't need a changelog entry, please remove it

@deekshacheruku deekshacheruku force-pushed the fix-flaky-json-simple-test branch from 54ec66b to 1c4329e Compare November 16, 2023 22:03
@deekshacheruku
Copy link
Author

@nedtwigg I have removed the changelog entry. Can you please elaborate on spotlessApply on what needs to be done? Thanks in advance.

@deekshacheruku deekshacheruku force-pushed the fix-flaky-json-simple-test branch from 1c4329e to 05496ef Compare November 20, 2023 18:41
@deekshacheruku
Copy link
Author

@nedtwigg I fixed the formatting issue! Let me know if there is anything else left. Thanks!

@nedtwigg nedtwigg enabled auto-merge November 20, 2023 21:25
@deekshacheruku
Copy link
Author

@nedtwigg There are some failing tests, Not sure why. Can you please have a look?

@@ -58,14 +59,14 @@ public static StepHarness forFormatter(Formatter formatter) {
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */
public StepHarness test(String before, String after) {
String actual = formatter.compute(LineEnding.toUnix(before), new File(""));
assertEquals(after, actual, "Step application failed");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets used for all kinds of tests, not only json tests. Also, it should be exactly the same as the snapshot, if it isn't, there's a bug in the formatter.

@nedtwigg
Copy link
Member

I haven't observed flakiness in the tests in question, and the fix is counter to the point of this library - exact formatting.

@nedtwigg nedtwigg closed this Nov 20, 2023
auto-merge was automatically disabled November 20, 2023 23:18

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants