EJB @PersistenceContext EntityManager Throws NullPointerException

假如想象 提交于 2019-12-02 10:21:16
Gabriel Aramburu

The EntityManager instance, is injected when the EJB is deployed in the Container. If you take a look at the lifecycle of enterprise bean, you will see clearly when dependency injection occurs. When the Container sees the @Persistencecontext annotation it will inject a container-managed EntityManager.

The problem is that the code executed in this test is not managed by the Container, therefore, no one inject the necessary dependencies.

bookDao = new BookEJB();

When you run the test, the BookEJB class is just a simple POJO, the @Stateless and @PersistenceContext annotations are simply ignored.

You have several alternatives in order to test your EJB, take a look at this link.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!