I'm trying to inject an entity manager into a DAO class and test it using a weld container, but I keep getting the following exception:
org.jboss.weld.exceptions.NullInstanceException: WELD-000044 Unable to get instance from org.jboss.weld.bean-se-module-ProducerField-com.playground.cdi_tutorial.beans.Resources.em on org.jboss.weld.bean .builtin.CallableMethodHandler.invoke (CallableMethodHandler.java:60) on org.jboss.weld.util.CleanableMethodHandler.invoke (CleanableMethodHandler.java:43) in javax.persistence.EntityManager _ $$_erytager _ javassist_2.java) on com.playground.cdi_tutorial.model.EventDAO.getAllEvents (EventDAO.java:22) on com.playground.cdi_tutorial.beans.ValidationService.validateEvenNumbers (ValidationService.java:23) on com.playground. 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.ref.five.reativefreff invoke (Nat iveMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:601) on org.junit.runners.model.Framework. 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) org .junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored (BlockJUnit4ClassRunner.unavaunrunnerunnerunnerunnerunnerunnerunnerunnerunnerunnerunnerunnerunlunclient.runnerblockerunnerunrunner.unava : 71) on org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:49) on org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:193) on org.junit.runners.ParentRunle $ 1. ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren (P arentRunner.java:191) at org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:42) at org.junit.runners.ParentRunner $ 2.value (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.TestExecutionr. .java: 38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTavaRun368 ) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.javaIf90) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197)
I am trying to enter an entity administrator 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 manufacturer class is as follows:
public class Resources { @Produces @EmProducer @PersistenceContext(unitName = EmProducer.UNIT_NAME) private EntityManager em; ... }
In my test, when this line of code falls into Query q = em.createQuery ("from Employee"); I get the exception above. However, the em object manager is not null.
PS: I'm using WeldJUnit4Runner (http://www.hostettler.net/blog/2012/04/02/how-to-test-a-jsf-named- bean /) to run my tests.
I am very grateful for your help.
Thanks!
Maria source share