Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.springframework.data.repository
* @return the entity with the given id or `null` if none found.
* @author Sebastien Deleuze
* @author Oscar Hernandez
* @author Subin Kim
* @since 2.1.4
*/
fun <T: Any, ID: Any> CrudRepository<T, ID>.findByIdOrNull(id: ID): T? = findById(id).orElse(null)
inline fun <T: Any, ID: Any> CrudRepository<T, ID>.findByIdOrNull(id: ID): T? = findById(id).orElse(null)
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package org.springframework.data.repository
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Test
import org.springframework.aop.framework.ProxyFactory
import org.springframework.data.repository.sample.User
import java.util.*

Expand All @@ -28,9 +29,14 @@ import java.util.*
*
* @author Sebastien Deleuze
* @author Mark Paluch
* @author Subin Kim
*/
class CrudRepositoryExtensionsTests {

private interface UserRepository : CrudRepository<User, String> {
override fun findById(id: String): Optional<User>
}

var repository = mockk<CrudRepository<User, String>>()

@Test // DATACMNS-1346
Expand All @@ -44,4 +50,22 @@ class CrudRepositoryExtensionsTests {
assertThat(repository.findByIdOrNull("foo")).isNull()
verify(exactly = 2) { repository.findById("foo") }
}

@Test // GH-3326
fun `findByIdOrNull should trigger AOP proxy on overridden method`() {

val mockTarget = mockk<UserRepository>()
val user = User()
every { mockTarget.findById("1") } returns Optional.of(user)

val factory = ProxyFactory()
factory.setTarget(mockTarget)
factory.addInterface(UserRepository::class.java)
val proxy = factory.proxy as UserRepository

val result = proxy.findByIdOrNull("1")
assertThat(result).isEqualTo(user)

verify(exactly = 1) { mockTarget.findById("1") }
}
}