users@glassfish.java.net

Re: EntityManager not being injected into abstract EJB?

From: Dominik Dorn <dominik.dorn_at_gmail.com>
Date: Wed, 14 Apr 2010 19:08:01 +0200

Well, I don't know about embedded glassfish, but I always create my
entityManager in the setupClass() of my tests and explicitly set the
created em to the dao.

automatic injection of the (correct) entityManager only works, if the
dao is created by the container... from what I've read, you're
creating the dao yourself (like I below), because else you would not
have to set the entitymanager by hand.

thats my code, maybe it helps:

public class Jpa2MemberDaoTest extends AbstractMemberDaoTest {
        private static EntityManagerFactory emf;

    private EntityManager em;

    @BeforeClass
    public static void createEntityManagerFactory() {
        emf = Persistence.createEntityManagerFactory("studyGuruPU");
    }

    @AfterClass
    public static void closeEntityManagerFactory() {
        emf.close();
    }

    @Before
    public void beginTransaction() {
        em = emf.createEntityManager();
        MemberFacade realDao = new MemberFacade();
        realDao.em = em;
        dao = realDao;
        em.getTransaction().begin();
   }

    @After
    public void rollbackTransaction() {

        if (em.getTransaction().isActive())
            em.getTransaction().rollback();

        if (em.isOpen())
            em.close();
    }
}