5 Replies Latest reply on Jan 19, 2010 6:28 AM by domagals.jame

    Unit Test: Could not find datasource: java:DefaultDS

    jhedden.jhedden.opsource.net

      I am getting the following error when trying to run a simple unit test:


        [testng] FATAL [org.hibernate.connection.DatasourceConnectionProvider] Could not find datasource: java:/DefaultDS
         [testng] java.lang.RuntimeException: PROVIDER_URL not provided in jndi.properties.  Automatic discovery not implemented yet.
         [testng]     at org.jboss.naming.JBossRemotingContextFactory.getInitialContext(JBossRemotingContextFactory.java:158)
         [testng]     at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
         [testng]     at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
         [testng]     at javax.naming.InitialContext.init(InitialContext.java:223)
         [testng]     at javax.naming.InitialContext.<init>(InitialContext.java:175)



      My test class looks like this:


      public class CustomerUnitTest {
              
              private EntityManagerFactory emf;
          
          public EntityManagerFactory getEntityManagerFactory() {
              return emf;
          }
          
          @BeforeClass
          public void init() {
              emf = Persistence.createEntityManagerFactory("ooord");
          }
          
          @AfterClass
          public void destroy() {
              emf.close();
          }
      
              @Test(groups = {"level.unit", "speed.fast"})
              public void unitTestCustomer() {
                      
                  Customer customer = new Customer();
                  customer.setOrgShortName("shortName");
                  customer.setOrgLongName("longName");
                  customer.setCustomerType(CustomerType.NEW);
                  
                  EntityManager em = getEntityManagerFactory().createEntityManager();
              em.getTransaction().begin();
                  em.persist(customer);
                  em.getTransaction().commit();
                  
                  Customer found = em.find(Customer.class ,customer.getId());
                  assertNotNull("find by id", found);
              assertEquals("id", customer.getId(), found.getId());
              assertEquals("orgShortName", "shortName", found.getOrgShortName());
              
                  em.close();
              }
      }



      My persistence.xml looks like this:


      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for tests -->
      <persistence 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_1_0.xsd" 
                   version="1.0">
                   
         <persistence-unit name="ooord">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>java:/DefaultDS</jta-data-source>
              <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                  <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                  <property name="hibernate.show_sql" value="true"/>
                  <property name="hibernate.cache.use_second_level_cache" value="false"/>
                  <property name="jboss.entity.manager.factory.jndi.name" value="java:/ooordEntityManagerFactory"/>           
                  </properties>
          </persistence-unit>
          
      </persistence>





      My ant script looks like this:


            <target name="buildtest" depends="compiletest,copytestclasses" description="Build the tests">
                  <copy todir="${test.dir}">
                      <fileset dir="${basedir}/resources">
                          <exclude name="META-INF/persistence*.xml"/>
                          <exclude name="${project.name}-*-ds.xml"/>
                      </fileset>
                  </copy>
                  <copy tofile="${test.dir}/META-INF/persistence.xml" file="${basedir}/resources/META-INF/persistence-test.xml" overwrite="true"/>
                  <copy tofile="${test.dir}/import.sql" file="${basedir}/sql/import-test.sql" overwrite="true"/>
                  <copy todir="${test.dir}" flatten="true">
                      <fileset dir="${src.test.dir}">
                          <include name="**/testng.xml" />
                      </fileset>
                  </copy>
              </target>
      
              <target name="test" depends="buildtest" description="Run the tests">            
                  <taskdef resource="testngtasks" classpath="${testng.jar}" />
                  <path id="test.path">
                      <path path="${test.dir}" />
                      <fileset dir="${lib.dir}/test">
                                 <include name="*.jar"/>
                      </fileset>
                      <path path="${bootstrap.dir}" />
                      <path refid="build.classpath" />
                  </path>
                  <testng outputdir="${basedir}/test-report">
                      <classpath refid="test.path" />
                      <xmlfileset dir="${test.dir}" includes="testng.xml" />
                  </testng>
               </target>



      Any help would be appreciated.  Its almost like it cant find hsqldb-ds.xml in the bootstrap.


        • 1. Re: Unit Test: Could not find datasource: java:DefaultDS
          jhedden.jhedden.opsource.net

          Looks like this is the answer:



          Use an integration test, rather than trying to test persistence in a unit test.



          • 2. Re: Unit Test: Could not find datasource: java:DefaultDS
            gjeudy

            You can use unit test structure to perform integration tests.


            Have you tried using:


            <property name="hibernate.connection.url" value="myurl"/>
                             <property name="hibernate.connection.driver_class" value="mydriver"/>
                         <property name="hibernate.connection.username" value="myuser"/>
                         <property name="hibernate.connection.password" value="mypass"/>



            instead of


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



            It does not seem like you are running jboss embedded container therefore you wouldnt have JNDI and even less so a datasource available in JNDI.


            Let me know how it goes.


            Guillaume

            • 3. Re: Unit Test: Could not find datasource: java:DefaultDS
              jhedden.jhedden.opsource.net

              It does not seem like you are running jboss embedded container therefore you wouldnt have JNDI and even less so a datasource available in JNDI.

              That was my suspicion.  That worked, Thanks.


              Im guessing then that I'll have 2 persistence-unit.  One for unit and one to go through the framework for Integration ala:



              <persistence-unit name="ooord-unit">
                      <provider>org.hibernate.ejb.HibernatePersistence</provider>
                      <properties>
                          <property name="hibernate.connection.url" value="jdbc:hsqldb:."/>
                          <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
                          <property name="hibernate.connection.username" value="sa"/>
                          <property name="hibernate.connection.password" value=""/>         
                      </properties>
                  </persistence-unit>
                  
                  <persistence-unit name="ooord-integration">
                      <provider>org.hibernate.ejb.HibernatePersistence</provider>
                      <jta-data-source>java:/DefaultDS</jta-data-source>
                      <properties>
                          <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                          <property name="hibernate.show_sql" value="true"/>
                          <property name="hibernate.cache.use_second_level_cache" value="false"/>
                          <property name="jboss.entity.manager.factory.jndi.name" value="java:/ooordEntityManagerFactory"/>           
                      </properties>
                  </persistence-unit>




              Or I could just test persistence through new ComponentTest().  Not sure of best practice.

              • 4. Re: Unit Test: Could not find datasource: java:DefaultDS
                gjeudy

                I think it's fine to have 2 persistent units defined as you long this persistence.xml is not included in the real prod build. I assume you locate your test persistence.xml in a test resource folder?


                BTW, I use Maven2 to build my project and it comes very handy for organizing tests.

                • 5. Re: Unit Test: Could not find datasource: java:DefaultDS

                  Thanks for posting this.  To my knowlege there is no document that states that you can do this instead of declaring a ....-ds.xml document for your connection.  You have helped me greatly.



                  Is this the only solution for getting the datasource to be found by tomcat than this? 


                  I have followed the instructions posted here, but still was not getting any luck,
                  http://seamframework.org/Documentation/RunningASeamgenProjectOnApacheTomcatWithoutEmbeddedJBoss


                  If you don't know, that is fine. I am just happy I finally go my project working on tomcat.