Skip to content

test(spanner): replace fixed hashCode value assertions with contract-compliance tests in OptionsTest#12901

Open
anishesg wants to merge 1 commit intogoogleapis:mainfrom
anishesg:fix/ph-issue-12268
Open

test(spanner): replace fixed hashCode value assertions with contract-compliance tests in OptionsTest#12901
anishesg wants to merge 1 commit intogoogleapis:mainfrom
anishesg:fix/ph-issue-12268

Conversation

@anishesg
Copy link
Copy Markdown

OptionsTest contained four assertions that verified Options#hashCode() returns specific fixed integer values (31, 108027089, -2118248262). The Java hashCode() contract does not guarantee cross-JVM or cross-version stability of hash values, so testing against hardcoded numbers is brittle and misleads readers into thinking fixed values are part of the API contract.

This change replaces each fixed-value assertion in allOptionsAbsent, listOptionsTest, updateOptionsTest, and transactionOptionsTest with contract-compliance assertions: two equal Options instances produce equal hash codes, and calling hashCode() twice on the same instance returns the same value (stability within a session).

Fixes #12268

…compliance tests in OptionsTest

`OptionsTest` contained four assertions that verified `Options#hashCode()` returns specific fixed integer values (31, 108027089, -2118248262). The Java `hashCode()` contract does not guarantee cross-JVM or cross-version stability of hash values, so testing against hardcoded numbers is brittle and misleads readers into thinking fixed values are part of the API contract.

Signed-off-by: anish k <ak8686@princeton.edu>
@anishesg anishesg requested review from a team as code owners April 23, 2026 16:47
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates OptionsTest.java to replace hardcoded hash code values with dynamic assertions that verify the hashCode contract between equal objects. The review feedback suggests removing redundant tautological assertions where an object's hash code is compared to itself and recommends using more idiomatic AssertJ assertions for object equality to provide better failure messages.

Comment on lines +155 to +159
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromReadOptions();
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The assertion assertThat(options.hashCode()).isEqualTo(options.hashCode()) is a tautology and provides little value in a unit test for a value-like object. Additionally, using assertThat(options).isEqualTo(options2) is more idiomatic and provides better failure messages than assertThat(options.equals(options2)).isTrue().

Suggested change
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromReadOptions();
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
// Verify hashCode contract: equal objects must have equal hashCodes.
Options options2 = Options.fromReadOptions();
assertThat(options).isEqualTo(options2);
assertThat(options.hashCode()).isEqualTo(options2.hashCode());

Comment on lines +182 to +188
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 =
Options.fromListOptions(
Options.pageSize(pageSize), Options.pageToken(pageToken), Options.filter(filter));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The assertion assertThat(options.hashCode()).isEqualTo(options.hashCode()) is a tautology and provides little value in a unit test for a value-like object. Additionally, using assertThat(options).isEqualTo(options2) is more idiomatic and provides better failure messages than assertThat(options.equals(options2)).isTrue().

Suggested change
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 =
Options.fromListOptions(
Options.pageSize(pageSize), Options.pageToken(pageToken), Options.filter(filter));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
// Verify hashCode contract: equal objects must have equal hashCodes.
Options options2 =
Options.fromListOptions(
Options.pageSize(pageSize), Options.pageToken(pageToken), Options.filter(filter));
assertThat(options).isEqualTo(options2);
assertThat(options.hashCode()).isEqualTo(options2.hashCode());

Comment on lines +709 to +713
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromUpdateOptions(Options.tag(tag));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The assertion assertThat(options.hashCode()).isEqualTo(options.hashCode()) is a tautology and provides little value in a unit test for a value-like object. Additionally, using assertThat(options).isEqualTo(options2) is more idiomatic and provides better failure messages than assertThat(options.equals(options2)).isTrue().

Suggested change
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromUpdateOptions(Options.tag(tag));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
// Verify hashCode contract: equal objects must have equal hashCodes.
Options options2 = Options.fromUpdateOptions(Options.tag(tag));
assertThat(options).isEqualTo(options2);
assertThat(options.hashCode()).isEqualTo(options2.hashCode());

Comment on lines +745 to +749
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromTransactionOptions(Options.tag(tag));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The assertion assertThat(options.hashCode()).isEqualTo(options.hashCode()) is a tautology and provides little value in a unit test for a value-like object. Additionally, using assertThat(options).isEqualTo(options2) is more idiomatic and provides better failure messages than assertThat(options.equals(options2)).isTrue().

Suggested change
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
Options options2 = Options.fromTransactionOptions(Options.tag(tag));
assertThat(options.equals(options2)).isTrue();
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
assertThat(options.hashCode()).isEqualTo(options.hashCode());
// Verify hashCode contract: equal objects must have equal hashCodes.
Options options2 = Options.fromTransactionOptions(Options.tag(tag));
assertThat(options).isEqualTo(options2);
assertThat(options.hashCode()).isEqualTo(options2.hashCode());

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.

[java-spanner] test: OptionsTest checks for fixed values being returned from hashCode()

1 participant