Skip to content

Commit 934394e

Browse files
committed
Document AOT workaround for @⁠PersistenceContext & @⁠PersistenceUnit in tests
Although @⁠PersistenceContext and @⁠PersistenceUnit are still not supported in tests running in AOT mode, as of Spring Framework 7.0, developers can inject an EntityManager or EntityManagerFactory into tests using @⁠Autowired instead of @⁠PersistenceContext and @⁠PersistenceUnit, respectively. See commit 096303c See gh-33414 Closes gh-31442
1 parent 798931e commit 934394e

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/aot.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ alternative, you can set the same property via the
4343
xref:appendix.adoc#appendix-spring-properties[`SpringProperties`] mechanism.
4444
====
4545

46+
[TIP]
47+
====
48+
JPA's `@PersistenceContext` and `@PersistenceUnit` annotations cannot be used to perform
49+
dependency injection within test classes in AOT mode.
50+
51+
However, as of Spring Framework 7.0, you can inject an `EntityManager` or
52+
`EntityManagerFactory` into tests using `@Autowired` instead of `@PersistenceContext` and
53+
`@PersistenceUnit`, respectively.
54+
====
55+
4656
[NOTE]
4757
====
4858
The `@ContextHierarchy` annotation is not supported in AOT mode.

spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ void endToEndTestsForEntireSpringTestModule() {
147147
// We only include test classes named *Tests so that we don't pick up
148148
// internal TestCase classes that aren't really tests.
149149
.filter(clazz -> clazz.getSimpleName().endsWith("Tests"))
150-
// TestNG EJB tests use @PersistenceContext which is not yet supported in tests in AOT mode.
150+
// TestNG EJB tests use @EJB which is not supported in tests in AOT mode, and
151+
// since @DisabledInAotMode is not able to disable TestNG tests at runtime,
152+
// we have to filter out those tests here.
151153
.filter(clazz -> !clazz.getPackageName().contains("testng.transaction.ejb"))
152154
// AOT processing works for ParameterizedDependencyInjectionTests by itself
153155
// but fails for an unknown reason within the entire spring-test module.

spring-test/src/test/java/org/springframework/test/context/testng/transaction/ejb/AbstractEjbTxDaoTestNGTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import jakarta.ejb.EJB;
2020
import jakarta.persistence.EntityManager;
21-
import jakarta.persistence.PersistenceContext;
2221
import org.testng.annotations.AfterMethod;
2322
import org.testng.annotations.Test;
2423

24+
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.test.annotation.DirtiesContext;
2626
import org.springframework.test.annotation.DirtiesContext.ClassMode;
2727
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@@ -45,7 +45,7 @@ abstract class AbstractEjbTxDaoTestNGTests extends AbstractTransactionalTestNGSp
4545
@EJB
4646
protected TestEntityDao dao;
4747

48-
@PersistenceContext
48+
@Autowired
4949
protected EntityManager em;
5050

5151

spring-test/src/test/java/org/springframework/test/context/transaction/ejb/AbstractEjbTxDaoTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
import jakarta.ejb.EJB;
2020
import jakarta.persistence.EntityManager;
21-
import jakarta.persistence.PersistenceContext;
21+
import jakarta.persistence.EntityManagerFactory;
2222
import org.junit.jupiter.api.AfterEach;
2323
import org.junit.jupiter.api.MethodOrderer;
2424
import org.junit.jupiter.api.Test;
2525
import org.junit.jupiter.api.TestMethodOrder;
2626

27+
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.test.annotation.DirtiesContext;
2829
import org.springframework.test.annotation.DirtiesContext.ClassMode;
2930
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -51,9 +52,14 @@ abstract class AbstractEjbTxDaoTests {
5152
@EJB
5253
protected TestEntityDao dao;
5354

54-
@PersistenceContext
55+
@Autowired
5556
protected EntityManager em;
5657

58+
// The EntityManagerFactory is not actually used by tests. We only declare it
59+
// to ensure that dependency injection works for it.
60+
@Autowired
61+
protected EntityManagerFactory emf;
62+
5763

5864
@Test
5965
void test1InitialState() {

0 commit comments

Comments
 (0)