Skip to content

Conversation

@cperkkk
Copy link
Contributor

@cperkkk cperkkk commented Sep 30, 2025

Fix for Issue #733

Fix Flaky Test in BasicHttpClientTest

What is the purpose of this PR?

This pull request addresses several flaky test, in the zerocode project. The test was failing intermittently due to assumptions about the ordering of fields returned by the code under test.

Test Affected

  • BasicHttpClientTest.createRequestBuilder
  • BasicHttpClientTest.createRequestBuilder_spaceInKeyValue
  • BasicHttpClientTest.createRequestBuilder_frontSlash
  • BasicHttpClientTest.createRequestBuilder_jsonValue

Why the test fails

The flakiness was caused by relying on field ordering from a java.util.Map (see BasicHttpClient.java#L305).

All test specified above relied on ordering returned by java.util.Map. It does not guarantee field order by default. This caused the test to fail when the field order changed, especially under NonDex shuffling, which explores non-deterministic behaviors.

How to reproduce the test failure

Run the test with NonDex to simulate shuffling of field order:

mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -pl core -Dtest=org.jsmart.zerocode.core.httpclient.BasicHttpClientTest

Before the fix, the test failed intermittently with errors similar to:

Expected: is "Company=Amazon&worthInBillion=999.999&age=30"
     but: was "age=30&worthInBillion=999.999&Company=Amazon"

Expected results

The test should pass consistently, regardless of the order of fields in the returned response.

Actual results

Before the fix, the test failed intermittently due to changes in the order of the returned response.

Description of fix

  1. Relaxed Assertions:
    • Replaced strict string comparisons with Map key-value checking. This ensures that field order does not affect the test results.
        assertThat(actualParams.get("Company"), is("Amazon"));
        assertThat(actualParams.get("age"), is("30"));
        assertThat(actualParams.get("worthInBillion"), is("999.999"));    

Validation

Test Results

  • Tests run normally: Passed

    mvn -pl core test -Dtest=org.jsmart.zerocode.core.httpclient.BasicHttpClientTest
image
  • Tests run with NonDex: Passed consistently across multiple seeds

    mvn edu.illinois:nondex-maven-plugin:2.1.1:nondex -pl core -Dtest=org.jsmart.zerocode.core.httpclient.BasicHttpClientTest

Additional Notes

  • The fix ensures the test is robust to non-deterministic behaviors of java.util.Map and compatible with future updates to it or related dependencies.

Let me know if further improvements or clarifications are needed!

@nirmalchandra nirmalchandra merged commit 9e83361 into authorjapps:master Oct 1, 2025
5 checks passed
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.

3 participants