This content has been marked as final.
Show 3 replies
-
1. Re: EntityManager is null on Arquillian test
bmajsak Mar 13, 2013 2:56 PM (in response to gpfurlaneto)Could you share the test code? Especially the deployment method - are you including beans.xml there?
-
2. Re: EntityManager is null on Arquillian test
gpfurlaneto Mar 14, 2013 8:12 AM (in response to bmajsak)This is my code.
@RunWith(Arquillian.class) public class MainTest { @Deployment() public static WebArchive createDeployment() { return ShrinkWrap.create(WebArchive.class, "test.war") .addClasses(Main.class, ServiceClass.class) .addAsResource("persistence.xml", "META-INF/persistence.xml") .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @EJB ServiceClass serviceClass; @PersistenceContext EntityManager em; @Test public void test1() { Assert.assertNotNull(serviceClass.getEntityManager()); Assert.assertNotNull(em); } } @Stateless public class ServiceClass { @PersistenceContext private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } }
Line 21 ( Assert.assertNotNull(serviceClass.getEntityManager()); ) is working perfectly, but at 22 ( Assert.assertNotNull(em); ) fails because the "em" is null.
Thanks.
-
3. Re: EntityManager is null on Arquillian test
aslak Mar 14, 2013 7:39 PM (in response to gpfurlaneto)@PersistenceContext injection in the TestClass is handled by CDI. In a WebArchive beans.xml is locted in WEB-INF, not META-INF.
use addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")