1+ /*
2+ * Copyright 2012-present the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package org.springframework.boot.jpa.test.autoconfigure
18+
19+ import jakarta.persistence.EntityManager
20+ import jakarta.persistence.EntityManagerFactory
21+ import jakarta.persistence.PersistenceUnitUtil
22+ import org.assertj.core.api.Assertions.assertThat
23+ import org.junit.jupiter.api.BeforeEach
24+ import org.junit.jupiter.api.Test
25+ import org.junit.jupiter.api.extension.ExtendWith
26+ import org.mockito.BDDMockito.given
27+ import org.mockito.BDDMockito.then
28+ import org.mockito.Mock
29+ import org.mockito.junit.jupiter.MockitoExtension
30+ import org.springframework.orm.jpa.EntityManagerHolder
31+ import org.springframework.transaction.support.TransactionSynchronizationManager
32+
33+ /* *
34+ * Tests for [TestEntityManager] Kotlin extensions.
35+ *
36+ * @author Beom Su
37+ * @since 4.1.0
38+ */
39+ @ExtendWith(MockitoExtension ::class )
40+ class TestEntityManagerExtensionsTests {
41+
42+ @Mock
43+ private lateinit var entityManagerFactory: EntityManagerFactory
44+
45+ @Mock
46+ private lateinit var entityManager: EntityManager
47+
48+ @Mock
49+ private lateinit var persistenceUnitUtil: PersistenceUnitUtil
50+
51+ private lateinit var testEntityManager: TestEntityManager
52+
53+ @BeforeEach
54+ fun setup () {
55+ this .testEntityManager = TestEntityManager (this .entityManagerFactory)
56+ }
57+
58+ @Test
59+ fun `find with reified type parameter` () {
60+ bindEntityManager()
61+ val entity = TestEntity ()
62+ given(this .entityManager.find(TestEntity ::class .java, 123 )).willReturn(entity)
63+ val result = this .testEntityManager.find<TestEntity >(123 )
64+ assertThat(result).isSameAs(entity)
65+ }
66+
67+ @Test
68+ fun `persistAndGetId with reified type parameter` () {
69+ bindEntityManager()
70+ val entity = TestEntity ()
71+ given(this .entityManagerFactory.persistenceUnitUtil).willReturn(this .persistenceUnitUtil)
72+ given(this .persistenceUnitUtil.getIdentifier(entity)).willReturn(123 )
73+ val result = this .testEntityManager.persistAndGetId<Int >(entity)
74+ then(this .entityManager).should().persist(entity)
75+ assertThat(result).isEqualTo(123 )
76+ }
77+
78+ @Test
79+ fun `getId with reified type parameter` () {
80+ val entity = TestEntity ()
81+ given(this .entityManagerFactory.persistenceUnitUtil).willReturn(this .persistenceUnitUtil)
82+ given(this .persistenceUnitUtil.getIdentifier(entity)).willReturn(123 )
83+ val result = this .testEntityManager.getId<Int >(entity)
84+ assertThat(result).isEqualTo(123 )
85+ }
86+
87+ private fun bindEntityManager () {
88+ val holder = EntityManagerHolder (this .entityManager)
89+ TransactionSynchronizationManager .bindResource(this .entityManagerFactory, holder)
90+ }
91+
92+ class TestEntity
93+
94+ }
0 commit comments