3 Replies Latest reply on Sep 23, 2010 11:13 AM by Andreas Doebeli

    entityManager test cases error

    elfuhrer Novice

      I saw a lot of people complaining that they can't get a hold of the entityManager in unit tests.

      If you have used seam-gen to generate your project, you have all you need to get started with writing your unit tests, with one little missing detail.

      Your generated seam project contains three different persistence scenarios, one is for production, one is for development and one is for testing.

      The ant build file uses the component-*.properties to populate your components.xml with the right values, this works for both dev and prod but the test environment has a small bug.

      The contents of the the components-test.properties are the following:

      # These properties are used to replace Ant-style tokens in the component descriptor (components.xml) at runtime.

      Now if you write a sample case to check the entityManager you would do it as follows:

      package myapp.test;
      import javax.persistence.EntityManager;
      import org.jboss.seam.mock.SeamTest;
      import org.testng.annotations.Test;
      public class EntityManagerTest extends SeamTest {
           public void testEntityManager() throws Exception {
                new ComponentTest() {
                     protected void testComponents() throws Exception {
                          EntityManager em = (EntityManager) getInstance("entityManager");
                                      assert em != null;

      If you run your tests using ant test or using testng plugin within eclipse, you'll get the following exception:

         [testng] FAILED: testEntityManager
         [testng] java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/entityManager
         [testng]      at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:245)
         [testng]      at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78)
         [testng]      at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107)
         [testng]      at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
         [testng]      at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
         [testng]      at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
         [testng]      at org.jboss.seam.Component.unwrap(Component.java:2275)
         [testng]      at org.jboss.seam.Component.getInstance(Component.java:2041)
         [testng]      at org.jboss.seam.Component.getInstance(Component.java:1983)
         [testng]      at org.jboss.seam.Component.getInstance(Component.java:1977)
         [testng]      at org.jboss.seam.Component.getInstance(Component.java:1972)
         [testng]      at org.jboss.seam.mock.AbstractSeamTest.getInstance(AbstractSeamTest.java:110)
         [testng]      at org.webpatterns.lab.seam.test.EntityManagerTest.access$0(EntityManagerTest.java:1)
         [testng]      at org.webpatterns.lab.seam.test.EntityManagerTest$1.testComponents(EntityManagerTest.java:14)
         [testng]      at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)
         [testng]      at org.webpatterns.lab.seam.test.EntityManagerTest.testEntityManager(EntityManagerTest.java:17)
         [testng] Caused by: javax.naming.NameNotFoundException: entityManager not bound
         [testng]      at org.jnp.server.NamingServer.getBinding(NamingServer.java:542)
         [testng]      at org.jnp.server.NamingServer.getBinding(NamingServer.java:550)
         [testng]      at org.jnp.server.NamingServer.getObject(NamingServer.java:556)
         [testng]      at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
         [testng]      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:669)
         [testng]      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
         [testng]      at javax.naming.InitialContext.lookup(InitialContext.java:409)
         [testng]      at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241)
         [testng]      ... 41 more
         [testng] ... Removed 26 stack frames
         [testng] ===============================================
         [testng]     EntityManagerTest
         [testng]     Tests run: 1, Failures: 1, Skips: 0
         [testng] ===============================================
         [testng] ===============================================
         [testng] MyApp Test Suite
         [testng] Total tests run: 1, Failures: 1, Skips: 0
         [testng] ===============================================

      This is caused because of the following entry in components-test.properties


      To resolve this set the puJndiName to the JNDI name of the entityManagerFactory defined in persistence.xml such as


      And now your unit tests run fine.

      I don't know if this workaround is the best solution, probably this should be taken into account from the build.xml file.

      I hope this helps whoever needs it.



        • 1. Re: entityManager test cases error
          Sean Wu Newbie
          Yep! I run into the same problem. It was due to the seam-gen template bug, you can fix it as follows:

          Edit: jboss-seam-2.2.0.GA\seam-gen\resources\components-test.properties
          change puJndiName to the second line. It will generate the right puJndiName when you create a new project with seam-gen.

          This should be filed to a JIRA.
          • 2. Re: entityManager test cases error
            Andreas Doebeli Newbie

            Actually, this was not enough for me. I additionally had to add the following property to persitence-test.xml:

            <persistence-unit name="@projectName@" transaction-type="JTA">
                  <property name="jboss.entity.manager.factory.jndi.name" value="java:/@projectName@EntityManagerFactory" />

            • 3. Re: entityManager test cases error
              Andreas Doebeli Newbie

              Edit: It was the file in jboss-seam-2.2.0.GA/seam-gen/resources/META-INF/persistence-test-war.xml