I am testing a Seam application using the needle test API. In my code I am using the getEntityManager() method from EntityHome. When I run the unit tests against an in memory database I get the following exception:
java.lang.IllegalStateException: No application context active
at org.jboss.seam.Component.forName(Component.java:1945)
at org.jboss.seam.Component.getInstance(Component.java:2005)
at org.jboss.seam.Component.getInstance(Component.java:1983)
at org.jboss.seam.Component.getInstance(Component.java:1977)
at org.jboss.seam.Component.getInstance(Component.java:1972)
at org.jboss.seam.framework.Controller.getComponentInstance(Controller.java:272)
at org.jboss.seam.framework.PersistenceController.getPersistenceContext(PersistenceController.java:20)
at org.jboss.seam.framework.EntityHome.getEntityManager(EntityHome.java:177)
etc ..
I can resolve some of these errors by injecting the EntityManager with
@In
EntityManager entityManager;
Unfortunately the persist method of EntityHome also calls the getEntityManager. This means a lot of mocks or rewriting the code somehow. Is there any workaround and why is this exception thrown anyway? I am using Seam 2.2.0 GA by the way.
There is nothing special about the components. They are generated by seam-gen. The test is performed with in memory database - I followed the examples in http://jbosscc-needle.sourceforge.net/jbosscc-needle/1.0/db-util.html.
In Seam what is the difference between injected EntityManager and getEntityManager from EntityHome ?
No one.
When using getEntityManager from EntityHome, Seam will lookup a Seam-managed persistence context named entityManager. If you set up your Seam-managed persistence context with other name than entityManager, your EntityHome will throw an Excedption.
Here is the default implementation of getEntityMananger in EntityHome
public EntityManager getEntityManager() {
return (EntityMananger) Component.getInstance("entityManager");
}
And when using a @In-jected EntityManager, Seam will do as shown above. Both are the same Seam managed persistence context.
Remember Seam performs lookup through @In-jected Component field name. For instance:
@In
EntityManager entityManager;
Because EntityManager field name is entityManager, Seam will perform a hierarchical search by using this name;
Here and here you can see how to set up a Seam Managed Persistence context.
regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With