From 70f64f2c269a05fb20695407d79c4de01e0baa59 Mon Sep 17 00:00:00 2001 From: Leo Li <269739606@qq.com> Date: Wed, 22 Sep 2021 11:43:08 +0800 Subject: [PATCH 1/2] Clarify use of @AutoConfigureTestEntityManager This commit makes it clearer that, when using @AutoConfigureTestEntityManager outside of @DataJpaTest, any tests using the test entity manager must be @Transactional. See gh-28086 --- .../src/docs/asciidoc/spring-boot-features.adoc | 2 +- .../boot/test/autoconfigure/orm/jpa/TestEntityManager.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 2230f98a92ba..2fb9e2c64f9e 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -7634,7 +7634,7 @@ If that is not what you want, you can disable transaction management for a test ---- Data JPA tests may also inject a {spring-boot-test-autoconfigure-module-code}/orm/jpa/TestEntityManager.java[`TestEntityManager`] bean, which provides an alternative to the standard JPA `EntityManager` that is specifically designed for tests. -If you want to use `TestEntityManager` outside of `@DataJpaTest` instances, you can also use the `@AutoConfigureTestEntityManager` annotation. +If you want to use `TestEntityManager` outside of `@DataJpaTest` instances, you can also use the `@AutoConfigureTestEntityManager` annotation, and make sure to use the `@Transactional` annotation on your test class or method. A `JdbcTemplate` is also available if you need that. The following example shows the `@DataJpaTest` annotation in use: diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java index b770ca8fa29f..94328f889341 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -234,7 +234,8 @@ public T getId(Object entity, Class idType) { */ public final EntityManager getEntityManager() { EntityManager manager = EntityManagerFactoryUtils.getTransactionalEntityManager(this.entityManagerFactory); - Assert.state(manager != null, "No transactional EntityManager found"); + Assert.state(manager != null, + "No transactional EntityManager found. Are your tests annotated with @Transactional?"); return manager; } From bedd749e1cddcd2c302f107171ed6ae28c4c7bd5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 1 Oct 2021 08:04:27 +0200 Subject: [PATCH 2/2] Polish "Clarify use of @AutoConfigureTestEntityManager" See gh-28086 --- .../src/docs/asciidoc/spring-boot-features.adoc | 6 +++++- .../boot/test/autoconfigure/orm/jpa/TestEntityManager.java | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 2fb9e2c64f9e..432010c9adfa 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -7634,7 +7634,11 @@ If that is not what you want, you can disable transaction management for a test ---- Data JPA tests may also inject a {spring-boot-test-autoconfigure-module-code}/orm/jpa/TestEntityManager.java[`TestEntityManager`] bean, which provides an alternative to the standard JPA `EntityManager` that is specifically designed for tests. -If you want to use `TestEntityManager` outside of `@DataJpaTest` instances, you can also use the `@AutoConfigureTestEntityManager` annotation, and make sure to use the `@Transactional` annotation on your test class or method. + +TIP: `TestEntityManager` can also be auto-configured to any of your Spring-based test class by adding `@AutoConfigureTestEntityManager`. +When doing so, make sure that your test is running in a transaction, for instance by adding `@Transactional` on your test class or method. + + A `JdbcTemplate` is also available if you need that. The following example shows the `@DataJpaTest` annotation in use: diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java index 94328f889341..96201adc8740 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java @@ -234,8 +234,7 @@ public T getId(Object entity, Class idType) { */ public final EntityManager getEntityManager() { EntityManager manager = EntityManagerFactoryUtils.getTransactionalEntityManager(this.entityManagerFactory); - Assert.state(manager != null, - "No transactional EntityManager found. Are your tests annotated with @Transactional?"); + Assert.state(manager != null, "No transactional EntityManager found, is your test running in a transactional?"); return manager; }