I am trying to inject an entity manager in a DAO class and test it, using weld container, but i keep getting the following exception:
org.jboss.weld.exceptions.NullInstanceException: WELD-000044 Unable to obtain instance from org.jboss.weld.bean-se-module-ProducerField-com.playground.cdi_tutorial.beans.Resources.em at org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:60) at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43) at javax.persistence.EntityManager_$$_javassist_2.createQuery(EntityManager_$$_javassist_2.java) at com.playground.cdi_tutorial.model.EventDAO.getAllEvents(EventDAO.java:22) at com.playground.cdi_tutorial.beans.ValidationService.validateEvenNumbers(ValidationService.java:23) at com.playground.cdi_tutorial.beans.MyFactory.sayHi(MyFactory.java:15) at com.playground.cdi_tutorial.beans.MyFactoryTest.should_say_bye(MyFactoryTest.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I try to inject the entity manager as follows:
@Inject
@EmProducer
EntityManager em;
where EmProducer looks like this:
@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface EmProducer {
public static final String UNIT_NAME = "cdi-tutorial";
}
And the producer class looks like this:
public class Resources {
@Produces
@EmProducer
@PersistenceContext(unitName = EmProducer.UNIT_NAME)
private EntityManager em;
...
}
In my test, when this line of code is hit Query q = em.createQuery("from Employee"); I get the exception above. The entity manager, em, is not null though.
PS: I use WeldJUnit4Runner (http://www.hostettler.net/blog/2012/04/02/how-to-test-a-jsf-named-bean/) to run my tests.
I would really appreciate your help.
Thanks!