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

Fix flaky tests in FieldNamingTest #2521

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

219sansim
Copy link

@219sansim 219sansim commented Nov 3, 2023

Purpose

The tests in FieldNamingTest are detected flaky because the gson.toJson() gives a non-deterministic output.
Related issue: #2520

Description

There is a need to modify the code such that the tests to do not expect the string to contain values in a particular order. After modifying the tests to check if the gson.toJson() output contains all expected substrings, the tests pass, instead of checking against the entire hardcoded string.

Reason:
The gson.toJson() internally calls com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields which makes a call to getDeclaredFields


And according to this stackoverlow discussion, getDeclaredFields in Java 8 does not return elements in a consistent order.

Reproduction of error

The test runs fail on 4/5 runs of the NonDex tool

Command to reproduce the failure:
mvn -pl gson edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=com.google.gson.functional.FieldNamingTest#<testname>

Example - test case testLowerCaseWithUnderscores

mvn -pl gson edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=com.google.gson.functional.FieldNamingTest#testLowerCaseWithUnderscores

Error Message:

[ERROR] com.google.gson.functional.FieldNamingTest.testLowerCaseWithUnderscores -- Time elapsed: 0.003 s <<< FAILURE!
value of: replace(...)
expected: {'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3,'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6,'annotatedName':7,'lower_id':8,'_9':9}
but was : {'annotatedName':7,'__upper_camel_leading_underscore':4,'lower_id':8,'upper_camel':2,'u_p_p_e_r__w_o_r_d_s':6,'lower_camel':1,'_9':9,'lower_words':5,'_lower_camel_leading_underscore':3}
	at com.google.gson.functional.FieldNamingTest.testLowerCaseWithUnderscores(FieldNamingTest.java:74)

Copy link

google-cla bot commented Nov 3, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@219sansim 219sansim marked this pull request as draft November 7, 2023 20:51
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.

1 participant