1 2 3 Previous Next 34 Replies Latest reply on May 24, 2011 8:12 AM by sirwayne

    How to Junit test EJB class

    Sam W Expert

      Hello,

       

      I am using Netbeans 7 with JBOSS 6 .

      When I execute the following Junit test, it thrown error.

       

      public class RoleFacadeTest0 {

       

          private static EJBContainer    ejbContainer;

          private static Context        ctx;

          private static Map          properties;

       

          public RoleFacadeTest0() {

          }

       

       

          @Before

          public void setUp() {

              properties = new HashMap();

              properties.put(EJBContainer.MODULES, new File("target/classes"));

              ejbContainer = EJBContainer.createEJBContainer(properties);

          }

       

          @Test

          public void testCreate() throws Exception {

              System.out.println("create");

              Role entity = new Role();

              entity.setRoleId(RandomNumberUtil.tinyId());

              entity.setRoleName("Admin");

       

              RoleFacade roleFacade = (RoleFacade) ctx.lookup("java:global/classes/RoleFacade");

              roleFacade.create(entity);

              Assert.assertEquals(roleFacade.count(),1);

          }

       

      The exception error is "Enable to instantiate container with factories." - on the red line as shown above.

       

      Can anyone please tell me how to correct the above error?

       

      Thanks

      Sam

        • 2. How to Junit test EJB class
          Sam W Expert

          Hello,

           

          I changed the Junit test class like below:

           

          @Before

              public void setUp() {

                  Map<String, String> config = new HashMap<String, String>();

                  config.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/houseware");

                  config.put("hibernate.connection.username", "root");

                  config.put("hibernate.connection.password", "admin");

                  config.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");

                  config.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

                  config.put("hibernate.show_sql", "true");

                  EntityManagerFactory emf

                          = Persistence.createEntityManagerFactory("HousewareEnterpriseApplication-v3-ejbPU", config);

                  em = emf.createEntityManager();

              }

           

          My persistence.xml file:

          <?xml version="1.0" encoding="UTF-8"?>

          <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

            <persistence-unit name="HousewareEnterpriseApplication-v3-ejbPU" transaction-type="RESOURCE_LOCAL">

              <provider>org.hibernate.ejb.HibernatePersistence</provider>

              <non-jta-data-source/>

              <properties>

                <property name="hibernate.hbm2ddl.auto" value="update"/>

              </properties>

            </persistence-unit>

          </persistence>

           

           

          But it throws exception:

          org/hibernate/proxy/EntityNotFoundDelegate

          java.lang.NoClassDefFoundError

          at org.hibernate.ejb.HIbernatePersistence.createEntityManagerFactory(HibernatePersitence.java:55)

          at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)

          at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest0.setUp(RoleFacadeTest0.java:48)

          ...

           

          Line 48 is the line which is highlited shown in my RoleFacadeTest0.java test file.

           

          Can anyone help?

           

          Thanks a lot

          Sam

          • 3. How to Junit test EJB class
            Nicklas Karlsson Master

            Try JBoss Arquillian for in-container tests

            • 4. How to Junit test EJB class
              Sam W Expert

              Thank you for the suggestion.

               

              Unfortunately I can't run JBOSS /Arquillian on my laptop because not enough memory.

              I really think this shouldn't be a rocket sicence for making Junit work with persistence tier.

               

              But my persistence.xml could be wrong:

               

              <?xml version="1.0" encoding="UTF-8"?>

              <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                <persistence-unit name="HousewareEnterpriseApplication-v3-ejbPU" transaction-type="RESOURCE_LOCAL">

                      <class>au.com.houseware.server.ejb.entity.Role</class>

                      <class>au.com.houseware.server.ejb.bean.RoleFacade</class>

                      <non-jta-data-source/>

                      <properties>

                              <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

                              <property name="javax.persistence.jdbc.url"

                                      value="jdbc:mysql://localhost:3306/houseware;create=true" />

                              <property name="javax.persistence.jdbc.user" value="root" />

                              <property name="javax.persistence.jdbc.password" value="admin" />

                      </properties>

                </persistence-unit>

              </persistence>

               

              my junit test file:

               

              emf = Persistence.createEntityManagerFactory("HousewareEnterpriseApplication-v3-ejbPU");

               

              Yes that s wring, it is only one line and this causes the following exception:

               

              Testsuite: au.com.houseware.server.ejb.entity.bean.RoleFacadeTest

              Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.601 sec

              Testcase: testCreate(au.com.houseware.server.ejb.entity.bean.RoleFacadeTest):          Caused an ERROR

              org/hibernate/proxy/EntityNotFoundDelegate

              java.lang.NoClassDefFoundError: org/hibernate/proxy/EntityNotFoundDelegate

                        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)

                        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)

                        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)

                        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)

                        at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.setUp(RoleFacadeTest.java:49)

              Caused by: java.lang.ClassNotFoundException: org.hibernate.proxy.EntityNotFoundDelegate

                        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

                        at java.security.AccessController.doPrivileged(Native Method)

                        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

                        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

                        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

                        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

               

              Test au.com.houseware.server.ejb.entity.bean.RoleFacadeTest FAILED

              test:

              Deleting: C:\Users\sam\AppData\Local\Temp\TEST-au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.xml

              BUILD SUCCESSFUL (total time: 3 seconds)

               

              Thanks

              Sam

              • 5. How to Junit test EJB class
                Sam W Expert

                After added ALL jboss client lib into my ejb module, running the junit test resulted the following more specific errors:

                 

                log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).

                log4j:WARN Please initialize the log4j system properly.

                ------------- ---------------- ---------------

                Testcase: testCreate(au.com.houseware.server.ejb.entity.bean.RoleFacadeTest):          Caused an ERROR

                Invalid persistence.xml.

                Error parsing XML (line-1 : column -1): cvc-complex-type.2.4.a: Invalid content was found starting with element 'non-jta-data-source'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, "http://java.sun.com/xml/ns/persistence":validation-mode, "http://java.sun.com/xml/ns/persistence":properties}' is expected.

                 

                 

                javax.persistence.PersistenceException: Invalid persistence.xml.

                Error parsing XML (line-1 : column -1): cvc-complex-type.2.4.a: Invalid content was found starting with element 'non-jta-data-source'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, "http://java.sun.com/xml/ns/persistence":validation-mode, "http://java.sun.com/xml/ns/persistence":properties}' is expected.

                 

                 

                          at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:145)

                          at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:145)

                          at org.hibernate.ejb.packaging.Per

                Test au.com.houseware.server.ejb.entity.bean.RoleFacadeTest FAILED

                test:

                Deleting: C:\Users\sam\AppData\Local\Temp\TEST-au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.xml

                BUILD SUCCESSFUL (total time: 2 minutes 20 seconds)

                 

                Sam

                • 6. How to Junit test EJB class
                  Sam W Expert

                  HOw here is some new exception:

                   

                  Testsuite: au.com.houseware.server.ejb.entity.bean.RoleFacadeTest

                  log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).

                  log4j:WARN Please initialize the log4j system properly.

                  Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 2.536 sec

                   

                   

                  ------------- Standard Error -----------------

                  log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).

                  log4j:WARN Please initialize the log4j system properly.

                  ------------- ---------------- ---------------

                  Testcase: testCreate(au.com.houseware.server.ejb.entity.bean.RoleFacadeTest):          Caused an ERROR

                  [PersistenceUnit: HousewareEnterpriseApplication-v3-ejbPU] Unable to build EntityManagerFactory

                  javax.persistence.PersistenceException: [PersistenceUnit: HousewareEnterpriseApplication-v3-ejbPU] Unable to build EntityManagerFactory

                            at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)

                            at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)

                            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)

                            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)

                            at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.setUp(RoleFacadeTest.java:49)

                  Caused by: org.hibernate.HibernateException: Could not find datasource

                            at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)

                            at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)

                            at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)

                            at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)

                            at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)

                            at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)

                            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)

                            at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)

                  Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                            at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)

                            at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

                            at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)

                            at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)

                            at javax.naming.InitialContext.lookup(InitialContext.java:392)

                            at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)

                   

                  Test au.com.houseware.server.ejb.entity.bean.RoleFacadeTest FAILED

                  test:

                  Deleting: C:\Users\sam\AppData\Local\Temp\TEST-au.com.houseware.server.ejb.entity.bean.RoleFacadeTest.xml

                  BUILD SUCCESSFUL (total time: 10 seconds)

                   

                  My persistence.xml file:

                   

                  <?xml version="1.0" encoding="UTF-8"?>

                  <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                    <persistence-unit name="HousewareEnterpriseApplication-v3-ejbPU" transaction-type="JTA">

                      <jta-data-source>java:/Houseware</jta-data-source>

                   

                   

                          <class>au.com.houseware.server.ejb.entity.bean.RoleFacade</class>

                          <exclude-unlisted-classes>false</exclude-unlisted-classes>

                          <properties>

                              <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/housewar"/>

                              <property name="hibernate.connection.username" value="root"/>

                              <property name="hibernate.connection.password" value="admin"/>

                              <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

                              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                              <property name="hibernate.show_sql" value="true"/>

                              <property name="hibernate.hbm2ddl.auto" value="update"/>

                          </properties>

                    </persistence-unit>

                  </persistence>

                   

                  What else have I missing?

                   

                  Thanks

                  Sam

                  • 7. Re: How to Junit test EJB class
                    David Heisterberg Newbie

                    JNDI isn't configured correctly, hence the Caused by: javax.naming.NoInitialContextException.  You probably need to set java.naming.factory.initial to whatever the JBoss context factory class is.  To be honest, I find Apache OpenEJB a lot easier to use for unit testing.  Here's a small example using an EJB with JPA2 entity manager injection -- unzip and run "mvn test".

                    • 8. How to Junit test EJB class
                      Sam W Expert

                      Thanks a lot David.

                      I will try this out.

                       

                      Sam.

                      • 10. How to Junit test EJB class
                        Sam W Expert

                        Hello Anish,

                         

                        Thank you for your response.

                        The tutorial of the opensourceframwrok looks ok, but it does not address how to conduct a real test at the persistence layer. I think OpenEJB is the way.I am still setting up my OpenEJB junit test case.

                         

                        Thanks

                        Sam

                        • 11. How to Junit test EJB class
                          Sam W Expert

                          Hello,

                           

                          With OpenEJB in my Junit test,  I got the following exception when running my test case:

                           

                          Testcase: testResourceInjection(au.com.houseware.server.ejb.entity.bean.RoleFacadeTest2):          Caused an ERROR

                          Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                          javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                                    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)

                                    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

                                    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)

                                    at javax.naming.InitialContext.bind(InitialContext.java:400)

                                    at au.com.houseware.server.ejb.entity.bean.OpenEJBTestBase.initializeOpenEJB(OpenEJBTestBase.java:16)

                           

                                    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)

                          Test au.com.houseware.server.ejb.entity.bean.RoleFacadeTest2 FAILED

                           

                           

                          Here is my test file:

                           

                           

                          @LocalClient

                          public class RoleFacadeTest2 extends OpenEJBTestBase {

                           

                           

                                    @Resource

                                    private UserTransaction userTX;

                           

                           

                                    @EJB

                                  private RoleFacade roleFacade;

                           

                           

                                    @Test

                                    public void testResourceInjection() {

                                  Assert.assertNotNull(userTX);

                                  Assert.assertNotNull(roleFacade);

                                    }

                           

                           

                          Any suggestion how to fix this error?

                           

                          Thanks

                          Sam

                          • 13. How to Junit test EJB class
                            Sam W Expert

                            One problem is I still don't know how to customize my persistence.xml file to work in Junit.

                             

                            Here is my persistence.xml file:

                             

                             

                            <?xml version="1.0" encoding="UTF-8"?>

                            <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                              <persistence-unit name="HousewareEnterpriseApplication-v3-ejbPU" transaction-type="RESOURCE_LOCAL">

                                <non-jta-data-source>jdbc/Houseware</non-jta-data-source>

                             

                             

                                   

                                    <class>au.com.houseware.server.ejb.entity.bean.RoleFacade</class>

                                    <exclude-unlisted-classes>false</exclude-unlisted-classes>

                                    <properties>

                                        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/housewar"/>

                                        <property name="hibernate.connection.username" value="root"/>

                                        <property name="hibernate.connection.password" value="admin"/>

                                        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

                                        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                                        <property name="hibernate.show_sql" value="true"/>

                                        <property name="hibernate.hbm2ddl.auto" value="update"/>

                                    </properties>

                              </persistence-unit>

                            </persistence>

                             

                             

                            but I don't even know whether it is getting called.

                             

                            Thanks

                            Sam

                            • 14. How to Junit test EJB class
                              Sam W Expert

                              jaikiran pai wrote:

                               

                              See http://community.jboss.org/wiki/GetquotjavaxnamingNoInitialContextExceptionquot

                               

                              Here is some new exceptoin after putting jnd.properties file in the class path:

                               

                               

                              Testcase: testCreate(au.com.houseware.server.ejb.entity.bean.RoleFacadeTest3):          Caused an ERROR

                              [PersistenceUnit: HousewareEnterpriseApplication-v3-ejbPU] Unable to build EntityManagerFactory

                              javax.persistence.PersistenceException: [PersistenceUnit: HousewareEnterpriseApplication-v3-ejbPU] Unable to build EntityManagerFactory

                                        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)

                                        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)

                                        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)

                                        at au.com.houseware.server.ejb.entity.bean.RoleFacadeTest3.setUp(RoleFacadeTest3.java:60)

                              Caused by: org.hibernate.HibernateException: Could not find datasource

                                        at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)

                                        at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)

                                        at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)

                                        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)

                                        at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)

                                        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)

                                        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)

                                        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)

                              Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory  [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory ]

                                        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)

                                        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

                                        at javax.naming.InitialContext.init(InitialContext.java:223)

                                        at javax.naming.InitialContext.<init>(InitialContext.java:175)

                                        at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:51)

                                        at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)

                              Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

                                        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

                                        at java.security.AccessController.doPrivileged(Native Method)

                                        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

                                        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

                                        at java.lang.Class.forName0(Native Method)

                                        at java.lang.Class.forName(Class.java:247)

                                        at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)

                                        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)

                               

                               

                              Thanks

                              Sam

                              1 2 3 Previous Next