When I try to access a global EntityManager instance from @Nested classes I get the following exception:
java.lang.IllegalArgumentException: JPA test must have either EntityManagerFactory or EntityManager field annotated with @PersistenceUnit, respectively @PersistenceContext
My class looks like this:
@ExtendWith(JpaUnit.class)
class NestedTest {
@PersistenceContext(unitName = "my-test-unit")
private EntityManager entityManager;
@Nested
@DisplayName("Given an empty test table")
class GivenAnEmptyTestTable {
@Test
@DisplayName("Then the size sould be zero")
void thenTheSizeShouldBeZero() {
List<Test> list = entityManager
.createQuery("select t from Test t", Test.class)
.getResultList();
assertEquals(list.size(), 0);
}
@Nested
@DisplayName("When a row is inserted")
class WhenARowIsInserted {
@BeforeEach
@Transactional(TransactionMode.COMMIT)
void setUp() {
Test t = new Test(1, 2, 3);
entityManager.persist(t);
}
@Test
@DisplayName("Then the size should be one")
void thenTheSizeShouldBeOne() {
List<Test> list = entityManager
.createQuery("select t from Test t", Test.class)
.getResultList();
assertEquals(list.size(), 0);
}
}
}
}
Is it somehow possible to keep the entityManager instance in the nested tests?
When I try to access a global EntityManager instance from @Nested classes I get the following exception:
java.lang.IllegalArgumentException: JPA test must have either EntityManagerFactory or EntityManager field annotated with @PersistenceUnit, respectively @PersistenceContext
My class looks like this:
Is it somehow possible to keep the entityManager instance in the nested tests?