3 Replies Latest reply on Jan 22, 2012 6:31 PM by aslak

    Injection of @PersistenceContext in CDI/Weld-Container

    ghilling

      Hi,

       

      I think this has been asked previously but the documentation seems not to be completely up-to-date and arquillian seems to evolve quite fast ...

      So maybe in the meantime I can get the following scenario to work:

       

      I want to test my CDI-Beans including JPA-Entities with arquillian. To have my unit-tests running fast I'd like to use the weld-ee-container (

      arquillian-weld-ee-embedded-1.1). With JBoss-7-remote everything works fine.

       

      Is this possible now and how do I have to setup my test container? The @PersistenceContext-Annotation will just be ignored. No error but also no EntityManager.

       

      Kind Regards,

       

      Gunnar

       

      Bean Code:


      @RequestScoped

      public class MathProvider {

        @Inject

                PowerProvider power;

       

        @PersistenceContext

                EntityManager entityManager;

       

        public double power(double d, double e) {

                          Person person = new Person();

                          person.setName("Gunnar");

                          entityManager.persist(person);

        entityManager.flush();

                          return power.power(d,e);

                }

      }

       

      Test Code:

       

      @RunWith(Arquillian.class)

      public class MathTest extends EntityEnabledTest {

        private static final double TINY_DELTA = 10E-10;

       

        @Inject

                private MathProvider math;

       

        @Deployment

                public static Archive<?> createTestArchive()

                {

                          return ShrinkWrap.create(JavaArchive.class, "test.jar")

                                              .addClasses(MathProvider.class, PowerProvider.class)

                                              .addAsManifestResource(new ByteArrayAsset("<beans/>".getBytes()),

                                                                  ArchivePaths.create("beans.xml"))

                                              .addAsManifestResource("META-INF/persistence.xml", "META-INF/persistence.xml");

                }

       

        @SuppressWarnings("unchecked")

        @Test

                public void testPower() {

                          assertEquals(27.0, math.power(3.0, 3.0), TINY_DELTA);

                          List<Person> result = proxyEntityManager.createQuery("select p from person p").getResultList();

                          assertEquals(1, result.size());

                          assertEquals("Gunnar", result.get(0).getName());

                }

      }