3 Replies Latest reply on Mar 14, 2013 7:39 PM by Aslak Knutsen

    EntityManager is null on Arquillian test

    Guilherme Furlaneto Newbie

      Hello, i'm a beginer into Arquillian and I'm working with those technologies:

       

      *jboss-as-7.1.1.Final

      *JSF 2

      *EJB3.1

       

      In my arquillian test class i need use EntityManager for complementary the tests, but the entityManager is ever null. I'm trying to use so:

       

      @PersistenceContext

      EntityManager em;

       

      @Test

      public void test1() {

              Assert.assertNotNull(em);

      }

       

      Into EJBs the entityManager is injected perfectly, only not into Arquillian tests classes.

       

      What i could do?

       

      thanks,

      Best Regards.

        • 1. Re: EntityManager is null on Arquillian test
          Bartosz Majsak Master

          Could you share the test code? Especially the deployment method - are you including beans.xml there?

          • 2. Re: EntityManager is null on Arquillian test
            Guilherme Furlaneto Newbie

            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 Knutsen Master

              @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")