Skip to content

Commit c5e0d1a

Browse files
EugeneNikchristophstrobl
authored andcommitted
DATAKV-186 - SimpleKeyValueRepository.existsById(…) now evaluates findById(…) results properly.
ExistsById now evaluates Optional.isPresent() instead of comparing findById result to null. Original Pull Request: #24
1 parent 256bb16 commit c5e0d1a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public Optional<T> findById(ID id) {
132132
*/
133133
@Override
134134
public boolean existsById(ID id) {
135-
return findById(id) != null;
135+
return findById(id).isPresent();
136136
}
137137

138138
/*

src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.keyvalue.repository;
1717

18+
import static org.junit.Assert.*;
1819
import static org.mockito.ArgumentMatchers.*;
1920
import static org.mockito.Mockito.*;
2021

@@ -124,6 +125,17 @@ public void findAllIds() {
124125
verify(opsMock, times(3)).findById(anyString(), eq(Foo.class));
125126
}
126127

128+
@Test // DATAKV-186
129+
public void existsById() {
130+
when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.empty());
131+
assertFalse(repo.existsById("one"));
132+
133+
when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.of(new Foo()));
134+
assertTrue(repo.existsById("one"));
135+
136+
verify(opsMock, times(2)).findById(anyString(), eq(Foo.class));
137+
}
138+
127139
@Test // DATACMNS-525
128140
public void findAllWithPageableShouldDelegateToOperationsCorrectlyWhenPageableDoesNotContainSort() {
129141

0 commit comments

Comments
 (0)