1 2 Previous Next 20 Replies Latest reply on Feb 27, 2006 7:31 PM by bill.burke

    Unit testing with Embeddable EJB 3.0 container

    rwallace

      I've gotten done porting over my Hibernate POJOs and annotating them for use in EJB3. Now I want to do some unit testing to make sure I did the mapping right. I've looked at the tutorial examples at http://docs.jboss.org/ejb3/embedded/embedded.html, but does anyone have any pointers on using it in Junit?

      Thanks,
      Rich

        • 1. Re: Unit testing with Embeddable EJB 3.0 container
          rwallace

          Nevermind, I just downloaded the alpha 3 embeddable ejb3 and there's a test-with-junit example in the embedded-tutorial directory.

          • 2. Re: Unit testing with Embeddable EJB 3.0 container
            rwallace

            I'm having some problems getting the unit tests to run properly. I'm running my unit tests from maven 2. To accomplish that I unjarred all the files in the lib directory of the jboss-EJB-3.0_Embeddable_ALPHA_3 zip file and created one big mega jar and installed that to my local repository. Now, when I try and run my unit test I get a ClassCastException. The unit test and the the stack trace are below. I think it may be an issue with the version of xerces that maven uses and the one that is bundled in the embeddable zip file. What do you guys think? Any ideas?

            Thanks,
            Rich

            package com.contentconnections.revman.book.test;
            
            import java.util.Hashtable;
            
            import javax.naming.InitialContext;
            import javax.persistence.EntityManager;
            import javax.transaction.TransactionManager;
            
            import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
            
            import com.contentconnections.revman.book.Publisher;
            
            import junit.framework.TestCase;
            
            public class PublisherPersistenceTest
             extends TestCase {
            
             public void setUp () {
             EJB3StandaloneBootstrap.boot (null);
             EJB3StandaloneBootstrap.scanClasspath ();
             }
            
             public void tearDown () {
             EJB3StandaloneBootstrap.shutdown ();
             }
            
             public void testAdd ()
             throws Exception {
             EntityManager em = (EntityManager) getInitialContext().lookup ("java:/EntityManagers/revman");
             TransactionManager tm = (TransactionManager) getInitialContext().lookup ("java:/TransactionManager");
            
             tm.begin();
            
             Publisher publisher = new Publisher ();
             publisher.setName ("Prentice Hall");
             publisher.setAbbreviation ("PH");
             em.persist (publisher);
            
             tm.commit ();
             }
            
             public static InitialContext getInitialContext() throws Exception {
             Hashtable props = getInitialContextProperties ();
             return new InitialContext (props);
             }
            
             private static Hashtable getInitialContextProperties () {
             Hashtable<String, String> props = new Hashtable<String, String>();
             props.put ("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
             props.put ("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
             return props;
             }
            }
            


            2005-11-12 14:05:08,808 0 DEBUG [main] org.jboss.kernel.KernelFactory (KernelFactory.java:68) - Starting JBoss Kernel construction...
            2005-11-12 14:05:08,891 83 DEBUG [main] org.jboss.kernel.KernelFactory (KernelFactory.java:94) - Completed JBoss Kernel construction. Duration: 79 milliseconds
            2005-11-12 14:05:08,936 128 ERROR [main] org.jboss.xb.binding.Util (Util.java:419) - Failed to create schema loader.
            java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl
             at org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance(Unknown Source)
             at org.jboss.xb.binding.Util.getXSImplementation(Util.java:414)
             at org.jboss.xb.binding.Util.loadSchema(Util.java:269)
             at org.jboss.xb.binding.sunday.unmarshalling.XsdBinder.bind(XsdBinder.java:100)
             at org.jboss.xb.binding.sunday.unmarshalling.XsdBinder.bind(XsdBinder.java:87)
             at org.jboss.kernel.plugins.deployment.xml.BeanSchemaBinding.readXsd(BeanSchemaBinding.java:997)
             at org.jboss.kernel.plugins.deployment.xml.BeanSchemaBinding.getSchemaBinding(BeanSchemaBinding.java:211)
             at org.jboss.kernel.plugins.deployment.xml.BeanXMLDeployer.<init>(BeanXMLDeployer.java:34)
             at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.boot(EJB3StandaloneBootstrap.java:209)
             at com.contentconnections.revman.book.test.PublisherPersistenceTest.setUp(PublisherPersistenceTest.java:19)
             at junit.framework.TestCase.runBare(TestCase.java:125)
             at junit.framework.TestResult$1.protect(TestResult.java:106)
             at junit.framework.TestResult.runProtected(TestResult.java:124)
             at junit.framework.TestResult.run(TestResult.java:109)
             at junit.framework.TestCase.run(TestCase.java:118)
             at junit.framework.TestSuite.runTest(TestSuite.java:208)
             at junit.framework.TestSuite.run(TestSuite.java:203)
             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:585)
             at org.codehaus.surefire.battery.JUnitBattery.executeJUnit(JUnitBattery.java:246)
             at org.codehaus.surefire.battery.JUnitBattery.execute(JUnitBattery.java:220)
             at org.codehaus.surefire.Surefire.executeBattery(Surefire.java:204)
             at org.codehaus.surefire.Surefire.run(Surefire.java:153)
             at org.codehaus.surefire.Surefire.run(Surefire.java:77)
             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:585)
             at org.codehaus.surefire.SurefireBooter.run(SurefireBooter.java:104)
             at org.apache.maven.test.SurefirePlugin.execute(SurefirePlugin.java:303)
             at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:399)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:519)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:469)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:448)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:301)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:268)
             at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:137)
             at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:316)
             at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:113)
             at org.apache.maven.cli.MavenCli.main(MavenCli.java:249)
             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:585)
             at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
             at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
             at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
             at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
            


            • 3. Re: Unit testing with Embeddable EJB 3.0 container
              rwallace

              Ok, well, I'm not sure exactly how but the ClassCastException problem seems to have worked itself out. Go figure.

              Now it gets past the call to boot() but dies in the call to scanClasspath() with

              Caused by: org.jboss.kernel.spi.registry.KernelRegistryEntryNotFoundException: Entry not found with name: InitialContextProperties
               at org.jboss.kernel.plugins.registry.AbstractKernelRegistry.getEntry(AbstractKernelRegistry.java:84)
               at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.getInitialContextProperties(EJB3StandaloneBootstrap.java:120)
               at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:138)
              


              Any ideas on this one?

              Thanks,
              Rich

              • 4. Re: Unit testing with Embeddable EJB 3.0 container
                melsom

                Hi

                Having the same problem trying to set up a JUnit Test with simple Entitybeans.

                Anyone that knows how to solve this issue?

                • 5. Re: Unit testing with Embeddable EJB 3.0 container
                  andrigtmiller

                  I am having the same problem. I have created my own Java Library in Eclipse, which includes all of the EJB3 Embedded jars, and put them in my build path inside Eclipse. Here is my JUnit test:

                  package test.ejb;
                  
                  import java.util.Hashtable;
                  
                  import javax.naming.InitialContext;
                  
                  import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
                  
                  import services.ejb.CustomerManager;
                  import services.entities.Customer;
                  
                  import junit.framework.TestCase;
                  
                  public class FindCustomerTest extends TestCase {
                  
                  
                   private CustomerManager customerManager;
                  
                   protected void setUp() throws Exception {
                  
                   super.setUp();
                  
                   EJB3StandaloneBootstrap.boot(null);
                   EJB3StandaloneBootstrap.scanClasspath();
                  
                   }
                  
                   protected void tearDown() throws Exception {
                  
                   super.tearDown();
                  
                   EJB3StandaloneBootstrap.shutdown();
                  
                   }
                  
                   public void testFindCustomer() throws Exception {
                  
                   Customer customerFound = null;
                  
                   InitialContext context = getInitialContext();
                  
                   customerFound = (Customer) context.lookup(Customer.class.getName());
                   customerFound = customerManager.findByPrimaryKey(1L);
                  
                   assertEquals("Found Customer number 1"
                   , customerFound.getCustomerId()
                   , 1L);
                  
                   }
                  
                   public static InitialContext getInitialContext() throws Exception {
                  
                   Hashtable props = getInitialContextProperties();
                  
                   return new InitialContext(props);
                  
                   }
                  
                   private static Hashtable getInitialContextProperties() {
                  
                   Hashtable<String, String> props = new Hashtable<String, String>();
                  
                   props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
                   props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                  
                   return props;
                  
                   }
                  
                  }
                  


                  I get the following exception trace when running the JUnit test from inside Eclipse:

                  java.lang.RuntimeException: org.jboss.kernel.spi.registry.KernelRegistryEntryNotFoundException: Entry not found with name: InitialContextProperties
                   at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:157)
                   at test.ejb.FindCustomerTest.setUp(FindCustomerTest.java:24)
                   at junit.framework.TestCase.runBare(TestCase.java:125)
                   at junit.framework.TestResult$1.protect(TestResult.java:106)
                   at junit.framework.TestResult.runProtected(TestResult.java:124)
                   at junit.framework.TestResult.run(TestResult.java:109)
                   at junit.framework.TestCase.run(TestCase.java:118)
                   at junit.framework.TestSuite.runTest(TestSuite.java:208)
                   at junit.framework.TestSuite.run(TestSuite.java:203)
                   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
                   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
                   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
                  Caused by: org.jboss.kernel.spi.registry.KernelRegistryEntryNotFoundException: Entry not found with name: InitialContextProperties
                   at org.jboss.kernel.plugins.registry.AbstractKernelRegistry.getEntry(AbstractKernelRegistry.java:84)
                   at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.getInitialContextProperties(EJB3StandaloneBootstrap.java:120)
                   at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:138)
                   ... 11 more
                  
                  


                  I don't really know where to look for the problem. Any help would be greatly appreciated.

                  • 6. Re: Unit testing with Embeddable EJB 3.0 container
                    bill.burke

                    Did you add all the xmlf files that are in the dist's conf/ directory?

                    I'm looking for a volunteer to create a sample Eclipse project that does a junit test. Interested?

                    • 7. Re: Unit testing with Embeddable EJB 3.0 container
                      andrigtmiller

                      No, I did not do that. If I add the XML files from the conf directory of the distribution to the project, how does the embedded container where to reference them. Given that, I think that I can make all this work. Also, I was considering publishing my results if I made this work anyway, because it seems like there are a lot of folks using the EJB3 implementation, and this would be a good first step in getting more folks comfortable with using it. I have a pretty good working prototype application now, and this is really the final step in putting it all together so that when this becomes a final release, I can guide my folks on how to us it.

                      • 8. Re: Unit testing with Embeddable EJB 3.0 container
                        bill.burke

                        How does the container find them?

                        ClassLoader.getResource("embedded-jboss-beans.xml");

                        Also, the deployer looks for META-INF/persistence.xml files when it scans the java.class.path System property to find things. I hope Eclipse sets java.class.path correctly?

                        I would really appreciate a sample Ecplise app. I don't use eclipse and really have no desire to learn it. Email me directly if you get a prototype junit test working from within eclipse:

                        bill@jboss.org

                        • 9. Re: Unit testing with Embeddable EJB 3.0 container
                          andrigtmiller

                          Bill,

                          Eclipse sets the workspace project in its classpath by default, so it should be able to find my persistence.xml file in my META-INF directory. I have extensively looked in this today. I will try a little later tonight on getting the conf XML files into my project, and see how that goes. If I get this working, I will document what the steps are, and will give you my prototype application.

                          Andy

                          • 10. Re: Unit testing with Embeddable EJB 3.0 container
                            andrigtmiller

                            Well, I have successfully been able to run a JUnit test against my EJB3 stateless session beans and entity beans. I have only one very simple test right now, but it does work! I do have one question though, which would make the approach to unit testing from inside Eclipse much easier and better. Is it possible to use the JBoss Tree Cache from the embedded container? The TreeCacheProviderHook class is there, but I cannot use it. I believe the EJB3EntityTreeCache mbean is not defined in the environment. I figure I can probably define it through the embedded-jboss-beans.xml file, but I am not sure how. If this is true, can someone at least point me in the right direction?

                            In terms of how I did this, here are the steps I took:

                            JBoss 4.0.3SP1 with EJB3 installed.
                            Eclipse 3.1.1
                            JBoss IDE 1.5RC1 installed.
                            Embedded EJB3 Alpha3 unziped to a directory of your choosing.

                            1) Create a new Java project (this is easier than doing within the same EJB 3 project).
                            2) When you setup the project reference the EJB 3 project on the "Projects" tab of your Java Build Path.
                            3) Add the JUnit libraries on the "Libraries" tab of your Java Build Path.
                            4) Outside of your project, go to the Window->Preferences->Java->Build Path->User Libraries preference.
                            5) Define a new User library by giving it a name (I called it Embedded EJB3), and point to the directory where you previously unziped the Embedded EJB3 distribution. Select all of the jar's in the lib and optional-lib directories for inclusion in your new User Library.
                            6) On your project properties, select the Java Build Path, and the Libraries tab on that, and add a User library. Select your newly created Embedded EJB3 library. This will bring in the entire Embedded EJB3 implementation into your project.
                            7) If you are not using the Hypersonic SQL embedded database, then also add to your project Java Build Path, the appropriate jar file for your JDBC driver. I am using MySQL.
                            8) In your project, import into your source directory the conf files from the Embedded EJB3 distribution (should be jboss-EJB-3.0_Embeddable_ALPHA_3/conf).
                            9) Customize the default.persistence.properties file to match what your EJB3 project has (datasource name, SQL Dialect, etc.)
                            10) Customize the embedded-jboss-beans.xml to match your datasource from you EJB3 project as well.
                            11) Write your JUnit tests, and you can now right click the java source file for your JUnit test, as long as there are no build problems, and select Run As->JUnit Test.

                            When you launch the JUnit test, it should pick up all of your entities and session beans, and anything else EJB3 from your EJB3 project, and pick up the embedded container configuration from your source folder on the new unit testing project. One thing to watch out for, is you cannot currently use the TreeCache that is illustrated for EJB3 usage in the embedded container (that was my question from above). I changed my persistence.xml file to use the Hibernate HashtableCacheProvider temporarily for my unit test.

                            Here are my customized files:

                            default.persistence.properties

                            hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
                            hibernate.connection.release_mode=after_statement
                            hibernate.transaction.flush_before_completion=false
                            hibernate.transaction.auto_close_session=false
                            hibernate.query.factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory
                            hibernate.hbm2ddl.auto=none
                            #hibernate.hbm2ddl.auto=create
                            hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
                            # Clustered cache with TreeCache
                            #hibernate.cache.provider_class=org.jboss.ejb3.entity.TreeCacheProviderHook
                            #hibernate.treecache.mbean.object_name=jboss.cache:service=EJB3EntityTreeCache
                            hibernate.connection.datasource=java:/MySqlDS
                            hibernate.dialect=org.hibernate.dialect.MySqlInnoDBDialect
                            hibernate.jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                            hibernate.jndi.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
                            


                            embedded-jboss-beans.xml

                            <?xml version="1.0" encoding="UTF-8"?>
                            
                            <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                             xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
                             xmlns="urn:jboss:bean-deployer">
                             <bean name="Naming" class="org.jnp.server.SingletonNamingServer"/>
                            
                             <bean name="InitialContextProperties" class="java.util.Hashtable">
                             <constructor>
                             <parameter class="java.util.Map">
                             <map keyClass="java.lang.String" valueClass="java.lang.String">
                             <entry>
                             <key>java.naming.factory.initial</key>
                             <value>org.jnp.interfaces.LocalOnlyContextFactory</value>
                             </entry>
                             <entry>
                             <key>java.naming.factory.url.pkgs</key>
                             <value>org.jboss.naming:org.jnp.interfaces</value>
                             </entry>
                             </map>
                             </parameter>
                             </constructor>
                             </bean>
                            
                             <bean name="JavaCompInitializer" class="org.jboss.ejb3.embedded.JavaCompInitializer">
                             <property name="jndiProperties"><inject bean="InitialContextProperties"/></property>
                             </bean>
                            
                             <bean name="XidFactory" class="org.jboss.tm.XidFactoryImpl"/>
                            
                             <bean name="XidFactoryMBean" class="org.jboss.ejb3.embedded.XidFactoryMBean">
                             <constructor>
                             <parameter class="org.jboss.tm.XidFactoryBase">
                             <inject bean="XidFactory"/>
                             </parameter>
                             </constructor>
                             </bean>
                            
                             <bean name="TransactionManagerInitializer" class="org.jboss.tm.TransactionManagerInitializer">
                             <property name="xidFactory"><inject bean="XidFactory"/></property>
                             <property name="initialContextProperties"><inject bean="InitialContextProperties"/></property>
                             </bean>
                            
                             <bean name="TransactionManager" class="java.lang.Object">
                             <constructor factoryMethod="getTransactionManager">
                             <factory bean="TransactionManagerInitializer"/>
                             </constructor>
                             </bean>
                             <bean name="CachedConnectionManager" class="org.jboss.resource.connectionmanager.CachedConnectionManagerReference">
                             <property name="transactionManager"><inject bean="TransactionManager"/></property>
                             </bean>
                            
                             <bean name="MySqlDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
                             <property name="driverClass">com.mysql.jdbc.Driver</property>
                             <property name="connectionURL">jdbc:mysql://localhost:3306/EJB3</property>
                             <property name="userName">EJB3</property>
                             <property name="password">test</property>
                             <property name="jndiName">java:/MySqlDS</property>
                             <property name="minSize">5</property>
                             <property name="maxSize">5</property>
                             <property name="blockingTimeout">1000</property>
                             <property name="idleTimeout">100000</property>
                             <property name="transactionManager"><inject bean="TransactionManager"/></property>
                             <property name="cachedConnectionManager"><inject bean="CachedConnectionManager"/></property>
                             <property name="initialContextProperties"><inject bean="InitialContextProperties"/></property>
                             </bean>
                            
                             <bean name="MySqlDS" class="java.lang.Object">
                             <constructor factoryMethod="getDatasource">
                             <factory bean="MySqlDSBootstrap"/>
                             </constructor>
                             </bean>
                            
                            
                            </deployment>
                            


                            unit test version of persistence.xml

                            <entity-manager>
                             <name>services</name>
                             <jta-data-source>java:/MySqlDS</jta-data-source>
                             <properties>
                             <property name="hibernate.hbm2ddl.auto" value="none"/>
                             <property name="hibernate.default_catalog" value="EJB3"/>
                             <property name="hibernate.jdbc.batch_size" value="10"/>
                            <!-- -->
                             <property name="hibernate.show_sql" value="true" />
                            
                             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
                            
                            <!-- <property name="hibernate.cache.provider_class"
                             value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
                             <property name="hibernate.treecache.mbean.object_name"
                             value="jboss.cache:service=EJB3EntityTreeCache"/>
                            -->
                            
                             <property name="hibernate.cache.provider_class"
                             value="org.hibernate.cache.HashtableCacheProvider"/>
                            
                             <property name="hibernate.ejb.classcache.services.entities.Customer" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.Inventory" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.Order" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.OrderLine" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.PricingMethod" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.Product" value="transactional"/>
                             <property name="hibernate.ejb.classcache.services.entities.ProductDemand" value="transactional"/>
                             </properties>
                            </entity-manager>
                            


                            I hope this helps others move forward, and I will be packaging up what I have done as a sample project with some documentation to walk you through it.

                            Any help on the TreeCache would be greatly appreciated.

                            Thanks.


                            • 11. Re: Unit testing with Embeddable EJB 3.0 container
                              bill.burke

                              You are the man!

                              • 12. Re: Unit testing with Embeddable EJB 3.0 container
                                melsom

                                Hi

                                I tried your "tutorial" but I get the same KernelRegistryEntryNotFoundException.

                                I have the same setup as you, but I use HSQLDB. Did you say you where going to packup your code for others to see?

                                • 13. Re: Unit testing with Embeddable EJB 3.0 container
                                  andrigtmiller

                                  Yeah, I am. I already made one improvement to what I originally had done. I put together both a test suite and test cases, where I was executing everything from the test suite. I haven't been able to work on this much lately, but will create a sample with instructions soon. I hope to have time later this week for that.

                                  Do you have the default.persistence.properties, ejb-interceptors-aop.xml, embedded-jboss-beans.xml, and log4j.xml files in your Eclipse project?

                                  • 14. Re: Unit testing with Embeddable EJB 3.0 container
                                    melsom

                                    Hi

                                    Hi

                                    I have gotten a bit further (just until now I got the time to do some work on this).

                                    Using MySQL, having this persistence.xml

                                    <entity-manager>
                                     <name>test</name>
                                     <jta-data-source>java:/MySqlDS</jta-data-source>
                                     <properties>
                                     <property name="hibernate.hbm2ddl.auto" value="none"/>
                                     <property name="hibernate.default_catalog" value="EJB3"/>
                                     <property name="hibernate.jdbc.batch_size" value="10"/>
                                     <property name="hibernate.show_sql" value="true" />
                                    
                                     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
                                    
                                     <property name="hibernate.cache.provider_class"
                                     value="org.hibernate.cache.HashtableCacheProvider"/>
                                    
                                     <property name="model.value.Person" value="transactional"/>
                                     </properties>
                                    </entity-manager>
                                    


                                    I get this exception:

                                    Exception in thread "main" java.lang.RuntimeException: unable to find: java:/managedEntityFactories/test
                                     at org.jboss.ejb3.injection.PersistenceContextHandler.getManagedEntityManagerFactory(PersistenceContextHandler.java:85)
                                     at org.jboss.ejb3.injection.PersistenceContextHandler.loadFieldInjectors(PersistenceContextHandler.java:168)
                                     at org.jboss.ejb3.injection.PersistenceContextHandler.loadInjectors(PersistenceContextHandler.java:134)
                                     at org.jboss.ejb3.EJBContainer.resolveInjectors(EJBContainer.java:486)
                                     at org.jboss.ejb3.EJBContainer.initializePool(EJBContainer.java:421)
                                     at org.jboss.ejb3.EJBContainer.start(EJBContainer.java:374)
                                     at org.jboss.ejb3.SessionContainer.start(SessionContainer.java:51)
                                     at org.jboss.ejb3.stateless.StatelessContainer.start(StatelessContainer.java:65)
                                     at org.jboss.ejb3.stateless.StatelessManager.startService(StatelessManager.java:98)
                                     at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:274)
                                     at org.jboss.system.ServiceMBeanSupport.start(ServiceMBeanSupport.java:181)
                                     at org.jboss.ejb3.embedded.EJB3StandaloneDeployment.start(EJB3StandaloneDeployment.java:96)
                                     at org.jboss.ejb3.embedded.EJB3StandaloneDeployer.start(EJB3StandaloneDeployer.java:389)
                                     at client.TestClient.main(TestClient.java:30)
                                    Caused by: javax.naming.NameNotFoundException: test not bound
                                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:516)
                                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:524)
                                     at org.jnp.server.NamingServer.getObject(NamingServer.java:530)
                                     at org.jnp.server.NamingServer.lookup(NamingServer.java:282)
                                     at org.jnp.server.NamingServer.lookup(NamingServer.java:256)
                                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
                                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
                                     at javax.naming.InitialContext.lookup(InitialContext.java:351)
                                     at org.jboss.ejb3.injection.PersistenceContextHandler.getManagedEntityManagerFactory(PersistenceContextHandler.java:81)
                                     ... 13 more
                                    


                                    This entity
                                    package model.value;
                                    
                                    import javax.persistence.Column;
                                    import javax.persistence.Entity;
                                    import javax.persistence.Id;
                                    import javax.persistence.Table;
                                    
                                    @Entity
                                    @Table(name="person")
                                    public class Person {
                                    
                                    
                                     private int id;
                                     private String givenname;
                                     private String surname;
                                    
                                    
                                     @Column(name="givenname")
                                     public String getGivenname() {
                                     return givenname;
                                     }
                                     public void setGivenname(String givenname) {
                                     this.givenname = givenname;
                                     }
                                    
                                     @Column(name="id")
                                     @Id
                                     public int getId() {
                                     return id;
                                     }
                                     public void setId(int id) {
                                     this.id = id;
                                     }
                                    
                                     @Column(name="surname")
                                     public String getSurname() {
                                     return surname;
                                     }
                                     public void setSurname(String surname) {
                                     this.surname = surname;
                                     }
                                    
                                    
                                    
                                    }
                                    
                                    


                                    and this main-class:

                                    public class TestClient {
                                     public static void main(String[] args) throws Exception
                                     {
                                     EJB3StandaloneBootstrap.boot(null);
                                    
                                     EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
                                     URL archive = getArchiveURL();
                                     deployer.getArchives().add(archive);
                                    
                                     // need to set the InitialContext properties that deployer will use
                                     // to initial EJB containers
                                     deployer.setJndiProperties(getInitialContextProperties());
                                    
                                     deployer.create();
                                     deployer.start();
                                    
                                     InitialContext ctx = getInitialContext();
                                     PersonServiceRemote remote = (PersonServiceRemote)ctx.lookup(PersonServiceRemote.class.getName());
                                    
                                     System.out.println("----------------------------------------------------------");
                                     System.out.println("This is the archive deployed from: ");
                                     System.out.print(" ");
                                     System.out.println(archive);
                                    
                                     List<Person> persons = remote.findAllPersons();
                                     Iterator i = persons.iterator();
                                     System.out.println(persons.size());
                                     while(i.hasNext()) {
                                     Person p = (Person)i.next();
                                     System.out.println(p.getGivenname());
                                     }
                                    
                                     deployer.stop();
                                     deployer.destroy();
                                     }
                                    
                                     public static URL getArchiveURL() throws Exception
                                     {
                                     // Usually you would hardcode your URL. This is just a way to make it easier for the tutorial
                                     // code to configure where the resource is.
                                     URL res = Thread.currentThread().getContextClassLoader().getResource("persistence.xml");
                                     return EJB3StandaloneDeployer.getContainingUrlFromResource(res, "persistence.xml");
                                     }
                                    
                                     public static InitialContext getInitialContext() throws Exception
                                     {
                                     Hashtable props = getInitialContextProperties();
                                     return new InitialContext(props);
                                     }
                                    
                                     private static Hashtable getInitialContextProperties()
                                     {
                                     Hashtable props = new Hashtable();
                                     props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
                                     props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                                     return props;
                                     }
                                    }
                                    
                                    
                                    

                                    Any ideas?

                                    1 2 Previous Next