Add test for putIfAbsent to catch implementations that incorrectly ignore null values#7987
Closed
lmcrean wants to merge 1 commit intogoogle:masterfrom
Closed
Add test for putIfAbsent to catch implementations that incorrectly ignore null values#7987lmcrean wants to merge 1 commit intogoogle:masterfrom
lmcrean wants to merge 1 commit intogoogle:masterfrom
Conversation
…s in `MapPutIfAbsentTester`. This new test ensures that when a null value is present for an existing key, the `putIfAbsent` method correctly replaces it with a new value. The test checks both the return value and the updated state of the map.
cpovirk
reviewed
Sep 14, 2025
Member
cpovirk
left a comment
There was a problem hiding this comment.
Thanks, I'll make sure that this doesn't break any internal users of the test suites (or fix them if it does) and then get it merged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6217
Problem
Multiple map implementations (e.g., fastutils'
Object2ObjectOpenHashMap) incorrectly pass Guava's testlib suite despite not properly implementingMap.putIfAbsentbehavior with null values. The JavaDoc specifies that putIfAbsent should replace a null value, but the testlib doesn't verify this edge case, allowing non-compliant implementations to pass.Solution
Add
testPutIfAbsent_replacesNullValue()to verify thatputIfAbsentcorrectly replaces existing null values with new values, returning null as specified in the Map interface documentation.Changes
MapPutIfAbsentTester.javathat:Testing
Breaking Changes
None. This only adds a new test case to catch non-compliant Map implementations.