Skip to content

Add test for putIfAbsent to catch implementations that incorrectly ignore null values#7987

Closed
lmcrean wants to merge 1 commit intogoogle:masterfrom
lmcrean:lmcrean-6217
Closed

Add test for putIfAbsent to catch implementations that incorrectly ignore null values#7987
lmcrean wants to merge 1 commit intogoogle:masterfrom
lmcrean:lmcrean-6217

Conversation

@lmcrean
Copy link
Contributor

@lmcrean lmcrean commented Sep 14, 2025

Fixes #6217

Problem

Multiple map implementations (e.g., fastutils' Object2ObjectOpenHashMap) incorrectly pass Guava's testlib suite despite not properly implementing Map.putIfAbsent behavior 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 that putIfAbsent correctly replaces existing null values with new values, returning null as specified in the Map interface documentation.

Changes

  • Added new test method in MapPutIfAbsentTester.java that:
    • Sets an existing key's value to null
    • Calls putIfAbsent with a new value
    • Verifies it returns null (the old value)
    • Confirms the key now maps to the new value

Testing

# Compile the testlib
./mvnw compile -pl guava-testlib

# Run tests that exercise MapPutIfAbsentTester with null-supporting maps
./mvnw test -Dtest=OpenJdk6MapTests -pl guava-testlib
./mvnw test -Dtest=HashMapTest -pl guava-tests
./mvnw test -Dtest=LinkedHashMapTest -pl guava-tests

Breaking Changes

None. This only adds a new test case to catch non-compliant Map implementations.

…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.
Copy link
Member

@cpovirk cpovirk left a comment

Choose a reason for hiding this comment

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

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.

@cpovirk cpovirk self-assigned this Sep 14, 2025
@cpovirk cpovirk added type=enhancement Make an existing feature better package=testing P2 labels Sep 14, 2025
copybara-service bot pushed a commit that referenced this pull request Sep 16, 2025
This new test ensures that when a null value is present, the `putIfAbsent` method replaces it with a new value. The test checks both the return value and the updated state of the map.

Fixes #6217
Fixes #7987

RELNOTES=n/a
PiperOrigin-RevId: 806970606
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 package=testing type=enhancement Make an existing feature better

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Testlib: Add test that putIfAbsent replaces a null value

3 participants