entityManager test cases error
elfuhrer Jun 10, 2010 4:44 AMI 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. jndiPattern=#{ejbName}/local debug=true seamBootstrapsPu=true seamEmfRef=#{entityManagerFactory} puJndiName=#{null}
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 { @Test public void testEntityManager() throws Exception { new ComponentTest() { protected void testComponents() throws Exception { EntityManager em = (EntityManager) getInstance("entityManager"); assert em != null; } }.run(); } }
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
puJndiName=#{null}
To resolve this set the puJndiName to the JNDI name of the entityManagerFactory defined in persistence.xml such as
puJndiName=java:/myAppEntityManagerFactory
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.
Cheers,
--fady