Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing global EntityManager instance in JUnit5 @Nested classes #46

Open
holyhoehle opened this issue Jan 21, 2019 · 1 comment
Open

Comments

@holyhoehle
Copy link

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?

@dadrus
Copy link
Owner

dadrus commented Jan 22, 2019

Hi,
This has not been implemented yet. Sorry. If you like you can submit a pull request. I'll be glad to see it.

ArneLimburg added a commit to ArneLimburg/jpa-unit that referenced this issue Nov 28, 2019
ArneLimburg added a commit to ArneLimburg/jpa-unit that referenced this issue Dec 2, 2019
ArneLimburg added a commit to ArneLimburg/jpa-unit that referenced this issue Dec 2, 2019
ArneLimburg added a commit to ArneLimburg/jpa-unit that referenced this issue Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants