users@glassfish.java.net

Embedded glassfish 3.1 - EJB JUnit Testing- EJB module and Entities are

From: <forums_at_java.net>
Date: Thu, 16 Jun 2011 10:08:47 -0500 (CDT)

Hi,   I am looking for help in doing the Junit test case for EJB module and
Entities using Embedded glassfish 3.1.   My project is a multi-module maven
project. My ear structure is below.    My ear      |       --- WAR
file       --- WAR file       --- EJB module (JAR file)      
--- lib         |          -- Entites (JAR file)     EJB
module and Entities are separate maven project and packaged as jar file. EJB
module has entities as its dependency. First I tried to test the ejb module
and entities alone, so I created the test case in EJB module. Included
‘glassfish-embedded-all’ in the pom file.   <dependency>     
<groupId>org.glassfish.extras</groupId>     
<artifactId>glassfish-embedded-all</artifactId>     
<version>3.1</version>      <scope>test</scope> </dependency>   Note:
EJB module has entities as dependency not in the EJB module itself.   Using
‘javax.ejb.embeddable.EJBContainer’ I tried to do the junit test case.
When I run the test case embedded glassfish is unable to find the
persistence.xml and entities module though they are included in the
classpath. Below is the error.   * * * *
*-----------------------------------------------------------------------------------------------------*
*javax.ejb.EJBException: EMF is null. Couldn't get extended EntityManager for
refName: com.papajohns.profit.businesslayer.OrderServicesBean/em; unitname:
Profit-entitiesPU* *            at
com.sun.ejb.containers.StatefulSessionContainer.createExtendedEMs(StatefulSessionContainer.java:691)*
*javax.naming.CommunicationException: Communication exception for
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is
javax.ejb.EJBException: EMF is null. Couldn't get extended EntityManager for
refName: com.papajohns.profit.businesslayer.OrderServicesBean/em; unitname:
Profit-entitiesPU]* *            at
com.sun.ejb.containers.StatefulSessionContainer.createBeanInstance(StatefulSessionContainer.java:584)*
*            at
com.sun.ejb.containers.StatefulSessionContainer.createEJBLocalBusinessObjectImpl(StatefulSessionContainer.java:532)*
*            at
com.sun.ejb.containers.BaseContainer.createEJBLocalBusinessObjectImpl(BaseContainer.java:2486)*
*            at
com.sun.ejb.containers.EJBLocalHomeImpl.createEJBLocalBusinessObjectImpl(EJBLocalHomeImpl.java:117)*
*            at
com.sun.ejb.containers.EJBLocalHomeInvocationHandler.invoke(EJBLocalHomeInvocationHandler.java:177)*
*            at $Proxy123.create(Unknown Source)*
*            at
com.sun.ejb.containers.JavaGlobalJndiNamingObjectProxy.create(JavaGlobalJndiNamingObjectProxy.java:65)*
*            at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:507)*
*            at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)*
*            at
javax.naming.InitialContext.lookup(InitialContext.java:392)*
*            at
javax.naming.InitialContext.lookup(InitialContext.java:392)*
*            at TestOrder.testCustomerEmail(TestOrder.java:71)*
*            at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)*
*            at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)*
*            at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)*
*            at java.lang.reflect.Method.invoke(Method.java:597)*
*            at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)*
*            at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)*
*            at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)*
*            at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)*
*            at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)*
*            at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)*
*            at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)*
*            at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)*
*            at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)*
*            at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)*
*            at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)*
*            at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)*
*            at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)*
*            at
org.junit.runners.ParentRunner.run(ParentRunner.java:236)*
*            at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)*
*            at
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:119)*
*            at
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:101)*
*            at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)*
*            at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)*
*            at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)*
*            at java.lang.reflect.Method.invoke(Method.java:597)*
*            at
org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)*
*            at $Proxy0.invoke(Unknown Source)*
*            at
org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)*
*            at
org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)*
*            at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)*
* *   When I included the persistence.xml and the entities source code to
the EJB module Junit test case ran successfully. Below is the test case
source code after including persistence.xml and entities source code in the
same EJB module.     Source code: ---------------   public class TestOrder
{       private static EJBContainer ejbContainer=null;     private
static Context ctx=null;     private static final String MODULE_NAME =
"embedded-classes";     private static final String TARGET_DIR = "target/"
+ MODULE_NAME;       private OrderServicesLocal orderservice;    
private static TransactionContext tc = new
TransactionContext("OrderServices");    @BeforeClass     public static
void setUp(){            File result = new File(TARGET_DIR);
        try {            FileUtils.copyDirectory(new
File("target/classes"), result);            
FileUtils.copyFile(new File("target/test-classes/META-INF/persistence.xml"),
           new File(TARGET_DIR + "/META-INF/persistence.xml"));
        } catch (IOException e) {            
e.printStackTrace(); //To change body of catch statement use File | Settings
| File Templates.         }           Map<String,Object> p=
new HashMap<String,Object>();             
p.put(EJBContainer.MODULES, result);             
p.put("org.glassfish.ejb.embedded.glassfish.web.http.port","");    
        ejbContainer= EJBContainer.createEJBContainer(p);
        ctx=ejbContainer.getContext();       }      
 @AfterClass     public static void tearDown(){          
ejbContainer.close();     }           @Test     public void
testCustomerEmail(){         try{     
                               
orderservice=(OrderServicesLocal)ctx.lookup("java:global/embedded-classes/OrderServicesBean!com.papajohns.profit.businesslayer.OrderServicesLocal");
            assertNotNull(orderservice);            
boolean result=orderservice.cancelOrder(tc,6426617,"In Person");
            System.out.println("Result *****"+result);
            assertTrue(result==true);          
}catch(Exception e){             System.out.println(e);
        }     }   }     Then I tried using the Glassfish
embedded api (ScatteredEnterpriseArchive) to deploy ear and test the junit
test case. Again I got the same error unable to find the persistence.xml and
entitites.   Why EJB Module is unable to find the persistence.xml and
entities module though they are in ejb module’s classpath?   Note : This
works fine when we run this application(not testcase) in Glassfish 3.1
server. When we deploy the ear, EJB module is able to identify the
persistence.xml and entities included as jar inside lib directory.   My ear
     |       --- WAR file       --- WAR file       --- EJB
module (JAR file)       --- lib         |          --
Entites (JAR file)   Kindly help me to resolve this issue.   Thanks,
Gladiator_FZ

--
[Message sent by forum member 'Gladiator_FZ']
View Post: http://forums.java.net/node/812613