Polish "Clarify use of @AutoConfigureTestEntityManager"

See gh-28086
This commit is contained in:
Stephane Nicoll 2021-10-01 08:04:27 +02:00
parent 70f64f2c26
commit bedd749e1c
2 changed files with 6 additions and 3 deletions

View File

@ -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:

View File

@ -234,8 +234,7 @@ public class TestEntityManager {
*/
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;
}