This extension allow you to run Jpa integration tests without having to start any EntityManagerFactory for each test class.
Add the following dependency to your project:
<dependency>
<groupId>io.github.jlmc</groupId>
<artifactId>jpa-junit5-extension</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
NOTE:
This project depends on:
- javax.persistence-api 2.2
- JUnit Jupiter 5.6.2
Add the @JpaTest
annotation to your test class. By default, Jpa EntityManagerFactory
will be started in the callback BeforeAllCallback#beforeAll
.
Using the @JpaTest
annotation to your test class, allows you to inject an instance of EntityManager
, EntityManagerFactory
or JpaProvider
for each execution of the test methods:
@JpaTest( persistenceUnit = "it" )
class JUnit5Test {
@JpaContext
private JpaProvider jpa;
@Test
void createBook() {
EntityManager em = jpa.em();
em.getTransaction().begin();
em.persist(new Book().setTitle("The Great Gatsby"));
em.getTransaction().commit();
em.close();
}
}