Class TestEntityManager
java.lang.Object
org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
Alternative to
EntityManager
for use in JPA tests. Provides a subset of
EntityManager
methods that are useful for tests as well as helper methods for
common testing tasks such as persist/flush/find
.- Since:
- 1.4.0
- Author:
- Phillip Webb
-
Constructor Summary
ConstructorDescriptionTestEntityManager
(EntityManagerFactory entityManagerFactory) Create a newTestEntityManager
instance for the givenEntityManagerFactory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the persistence context, causing all managed entities to become detached.void
Remove the given entity from the persistence context, causing a managed entity to become detached.<E> E
Find by primary key.void
flush()
Synchronize the persistence context to the underlying database.final EntityManager
Return the underlyingEntityManager
that's actually used to perform all operations.Return the ID of the given entity.<T> T
Return the ID of the given entity cast to a specific type.<E> E
merge
(E entity) Merge the state of the given entity into the current persistence context.<E> E
persist
(E entity) Make an instance managed and persistent.<E> E
persistAndFlush
(E entity) Make an instance managed and persistent then synchronize the persistence context to the underlying database.persistAndGetId
(Object entity) Make an instance managed and persistent then return its ID.<T> T
persistAndGetId
(Object entity, Class<T> idType) Make an instance managed and persistent then return its ID.<E> E
persistFlushFind
(E entity) Make an instance managed and persistent, synchronize the persistence context to the underlying database and finally find the persisted entity by its ID.<E> E
refresh
(E entity) Refresh the state of the instance from the database, overwriting changes made to the entity, if any.void
Remove the entity instance.
-
Constructor Details
-
TestEntityManager
Create a newTestEntityManager
instance for the givenEntityManagerFactory
.- Parameters:
entityManagerFactory
- the source entity manager factory
-
-
Method Details
-
persistAndGetId
Make an instance managed and persistent then return its ID. Delegates toEntityManager.persist(Object)
thengetId(Object)
.Helpful when setting up test data in a test:
Object entityId = this.testEntityManager.persist(new MyEntity("Spring"));
- Parameters:
entity
- the source entity- Returns:
- the ID of the newly persisted entity
-
persistAndGetId
Make an instance managed and persistent then return its ID. Delegates toEntityManager.persist(Object)
thengetId(Object, Class)
.Helpful when setting up test data in a test:
Long entityId = this.testEntityManager.persist(new MyEntity("Spring"), Long.class);
- Type Parameters:
T
- the ID type- Parameters:
entity
- the source entityidType
- the ID type- Returns:
- the ID of the newly persisted entity
-
persist
public <E> E persist(E entity) Make an instance managed and persistent. Delegates toEntityManager.persist(Object)
then returns the original source entity.Helpful when setting up test data in a test:
MyEntity entity = this.testEntityManager.persist(new MyEntity("Spring"));
- Type Parameters:
E
- the entity type- Parameters:
entity
- the entity to persist- Returns:
- the persisted entity
-
persistFlushFind
public <E> E persistFlushFind(E entity) Make an instance managed and persistent, synchronize the persistence context to the underlying database and finally find the persisted entity by its ID. Delegates topersistAndFlush(Object)
thenfind(Class, Object)
with theentity ID
.Helpful when ensuring that entity data is actually written and read from the underlying database correctly.
- Type Parameters:
E
- the entity type- Parameters:
entity
- the entity to persist- Returns:
- the entity found using the ID of the persisted entity
-
persistAndFlush
public <E> E persistAndFlush(E entity) Make an instance managed and persistent then synchronize the persistence context to the underlying database. Delegates toEntityManager.persist(Object)
thenflush()
and finally returns the original source entity.Helpful when setting up test data in a test:
MyEntity entity = this.testEntityManager.persistAndFlush(new MyEntity("Spring"));
- Type Parameters:
E
- the entity type- Parameters:
entity
- the entity to persist- Returns:
- the persisted entity
-
merge
public <E> E merge(E entity) Merge the state of the given entity into the current persistence context. Delegates toEntityManager.merge(Object)
- Type Parameters:
E
- the entity type- Parameters:
entity
- the entity to merge- Returns:
- the merged entity
-
remove
Remove the entity instance. Delegates toEntityManager.remove(Object)
- Parameters:
entity
- the entity to remove
-
find
Find by primary key. Delegates toEntityManager.find(Class, Object)
.- Type Parameters:
E
- the entity type- Parameters:
entityClass
- the entity classprimaryKey
- the entity primary key- Returns:
- the found entity or
null
if the entity does not exist - See Also:
-
flush
public void flush()Synchronize the persistence context to the underlying database. Delegates toEntityManager.flush()
. -
refresh
public <E> E refresh(E entity) Refresh the state of the instance from the database, overwriting changes made to the entity, if any. Delegates toEntityManager.refresh(Object)
.- Type Parameters:
E
- the entity type- Parameters:
entity
- the entity to refresh- Returns:
- the refreshed entity
-
clear
public void clear()Clear the persistence context, causing all managed entities to become detached. Delegates toEntityManager.clear()
-
detach
Remove the given entity from the persistence context, causing a managed entity to become detached. Delegates toEntityManager.detach(Object)
.- Parameters:
entity
- the entity to detach.
-
getId
Return the ID of the given entity. Delegates toPersistenceUnitUtil.getIdentifier(Object)
.- Parameters:
entity
- the source entity- Returns:
- the ID of the entity or
null
- See Also:
-
getId
Return the ID of the given entity cast to a specific type. Delegates toPersistenceUnitUtil.getIdentifier(Object)
.- Type Parameters:
T
- the ID type- Parameters:
entity
- the source entityidType
- the expected ID type- Returns:
- the ID of the entity or
null
- See Also:
-
getEntityManager
Return the underlyingEntityManager
that's actually used to perform all operations.- Returns:
- the entity manager
-