DATACMNS-1346 - Add a findByIdOrNull(…) extension to CrudRepository.
In Kotlin, it is idiomatic to deal with return value that could have or not a result with nullable types since they are natively supported by the language. This commit adds CrudRepository.findByIdOrNull(…) variant to CrudRepository#findById that returns T? instead of Optional<T>. Original pull request: #299.
This commit is contained in:
committed by
Oliver Drotbohm
parent
0a85e44dc9
commit
e48bcf6e84
@@ -0,0 +1,10 @@
|
||||
package org.springframework.data.repository
|
||||
|
||||
/**
|
||||
* Retrieves an entity by its id.
|
||||
*
|
||||
* @param id the entity id.
|
||||
* @return the entity with the given id or `null` if none found
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
fun <T, ID> CrudRepository<T, ID>.findByIdOrNull(id: ID): T? = findById(id).orElse(null)
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.springframework.data.repository
|
||||
|
||||
import com.nhaarman.mockito_kotlin.verify
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.springframework.data.repository.sample.User
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class CrudRepositoryExtensionsTests {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||
lateinit var repository: CrudRepository<User, String>
|
||||
|
||||
@Test
|
||||
fun `CrudRepository#findByIdOrNull() extension should call its Java counterpart`() {
|
||||
repository.findByIdOrNull("foo")
|
||||
verify(repository).findById("foo")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user