8 Replies Latest reply on Feb 13, 2007 1:38 PM by matt.drees

    unit tests in a seam-genned app

    matt.drees

      I seam-genned a blank app (called booking), then seam-genned a form (RegisterAction). I wanted to create a unit test (as in the Seam docs (http://docs.jboss.com/seam/1.1.5.GA/reference/en/html/testing.html#d0e13075)), so I copy/pasted the code and tweaked some things:

      public class RegisterActionTest {
      
       @Test
       public void testRegisterAction()
       {
       }
      
       private EntityManagerFactory emf;
      
       public EntityManagerFactory getEntityManagerFactory()
       {
       return emf;
       }
      
       @Configuration(beforeTestClass=true)
       public void initialize()
       {
       emf = Persistence.createEntityManagerFactory("booking");
       }
      
       @Configuration(afterTestClass=true)
       public void destroy()
       {
       emf.close();
       }
      }
      


      Running "ant test" gives me a
      javax.naming.NamingException: Local server is not initialized
      error in initialize().

      If I extend SeamTest (which I think shouldn't be necessary for a unit test), I get a
      java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
      in initialize().

      I think Seam-gen must not be including an EHCache jar, but I couldn't find where it is supposed to come from (or why the seam-genned integration test doesn't fail).

      Also, should unit tests be required to extend SeamTest?

      I'm running Seam from cvs.
      Thanks for your help.

        • 1. Re: unit tests in a seam-genned app
          gavin.king

          I think this was a bug in 1.1.1 that is fixed in 1.1.5.

          • 2. Re: unit tests in a seam-genned app
            matt.drees

            I was running from cvs head, so I'm assuming everything in 1.1.5 is in my copy.

            But I also tried 1.1.5; same behavior.

            • 3. Re: unit tests in a seam-genned app
              gavin.king

              Oh I didnt read properly. The reason for the error is that the persistence.xml file is using a JTA datasource, which of course does not exist in a unit test. You need a different persistence.xml if you want to run in a unit test.

              • 4. Re: unit tests in a seam-genned app
                matt.drees

                Ok, thanks.

                I'm not sure that any of the examples show how to set up a non-JTA datasource; do they? Is there somewhere else I learn how to do this?

                Thanks

                • 5. Re: unit tests in a seam-genned app
                  gavin.king

                  Check the HEM docs.

                  • 6. Re: unit tests in a seam-genned app
                    matt.drees

                    Thanks.

                    From the HEM docs, I put this together:

                     <persistence-unit name="bookingLocal" transaction-type="RESOURCE_LOCAL">
                     <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
                     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
                     <property name="hibernate.connection.username" value="sa"/>
                     <property name="hibernate.connection.password" value=""/>
                     <property name="hibernate.connection.url" value="jdbc:hsqldb:."/>
                     <property name="hibernate.max_fetch_depth" value="3"/>
                     </properties>
                     </persistence-unit>
                    


                    I was still getting the NoClassDefFoundError for CacheException, though. So I just grabbed the jar from hibernate core (downloaded separately) and put it in lib. Then it ran fine.

                    • 7. Re: unit tests in a seam-genned app
                      clivehackney

                      Matt,

                      I was having the same issue, but I fixed it buy adding the following line to the none jta datasource you set up.

                       <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                      


                      so your booking local source should read.

                      <persistence-unit name="bookingLocal" transaction-type="RESOURCE_LOCAL">
                       <properties>
                       <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
                       <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
                       <property name="hibernate.connection.username" value="sa"/>
                       <property name="hibernate.connection.password" value=""/>
                       <property name="hibernate.connection.url" value="jdbc:hsqldb:."/>
                       <property name="hibernate.max_fetch_depth" value="3"/>
                       <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                       </properties>
                       </persistence-unit>


                      I went back and tried the original persistence-test.xml file, which I added the above line to, this also fixed the tests. It appears the the bootstrap bean in the jboss-beans.xml dosn't pick this up.

                      my persistence-test.xml now reads.
                      <persistence-unit name="bookings">
                       <provider>org.hibernate.ejb.HibernatePersistence</provider>
                       <jta-data-source>java:/bookingsTestDatasource</jta-data-source>
                       <properties>
                       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                       <property name="hibernate.show_sql" value="true"/>
                       <property name="jboss.entity.manager.factory.jndi.name" value="java:/bookingsEntityManagerFactory"/>
                       <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                       </properties>
                       </persistence-unit>
                      


                      Hope that makes some sense.

                      Clive


                      • 8. Re: unit tests in a seam-genned app
                        matt.drees

                        Cool. Thanks Clive.