Mock the Map.getOrDefault(key, defaultValue) operation.

This commit is contained in:
John Blum
2020-09-16 12:20:28 -07:00
parent 2e94141994
commit 74634337fb
2 changed files with 20 additions and 1 deletions

View File

@@ -2642,6 +2642,17 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
})
.orElse(null));
// Map.getOrDefault(key, defaultValue)
doAnswer(invocation -> {
Object key = invocation.getArgument(0);
Object value = data.get(key);
Object defaultValue = invocation.getArgument(1);
return value != null ? value : defaultValue;
}).when(mockRegion).getOrDefault(any(), any());
// Region.invalidate(key)
doAnswer(invocation -> {

View File

@@ -59,7 +59,6 @@ import org.apache.geode.cache.LoaderHelper;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.RegionService;
import org.apache.geode.internal.cache.LoaderHelperImpl;
import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport;
import org.springframework.data.gemfire.tests.support.MapBuilder;
@@ -613,4 +612,13 @@ public class MockRegionDataAccessOperationsAndEventsUnitTests {
verify(mockRegionAttributes, times(2)).getCacheLoader();
verifyNoMoreInteractions(mockCacheLoader);
}
@Test
public void mapGetOrDefaultIsCorrect() {
this.mockRegion.put(1, "TEST");
assertThat(this.mockRegion.getOrDefault(1, "MOCK")).isEqualTo("TEST");
assertThat(this.mockRegion.getOrDefault(2, "MOCK")).isEqualTo("MOCK");
}
}