3 Replies Latest reply on Feb 16, 2010 8:28 PM by Khosro Asgharifard Sharabiani

    Testing Seam component and connect to DB in test case class

    Khosro Asgharifard Sharabiani Newbie

      Hello ,
      I want to write test case ,and use it for testing Seam component.For testing my Seam component,i must connect to database and save a entity to it.
      Also ,i have looked at this link http://seamframework.org/Community/TestNGWithSeamTest ,but i could not find a solution for my problem.
      In order to do this i use this codes


      Test class :


      public class NewTest extends SeamTest {
           BookDAO bookDAO;
           @Test
           public void f() {
                EntityManagerFactory emf = Persistence
                          .createEntityManagerFactory("aut");
                EntityManager em = emf.createEntityManager();
                System.out.println(em);
                /*BookDAO bookDAO= (BookDAO) Component.getInstance(BookDAO.class);
                System.out.println(bookDAO);*/
           }
      }
      



      persistence.xml


        <persistence-unit name="aut" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <non-jta-data-source>java:/autcmsDatasource</non-jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.binary_or_blob" value="blob"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               <!--
               <property name="hibernate.hbm2ddl.auto" value="none"/> 
                -->
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
             </properties>
         </persistence-unit>
       </persistence>
      


      component.xml


           <core:init debug="false" jndi-pattern="@jndiPattern@" />
      



      I do not know how to say to test class that use these xml files
      And i put all them in package test,but when i run test i got this exception


      C:\Users\khosro\AppData\Local\Temp\testng-eclipse\testng-customsuite.xml
      
      - Could not find datasource: java:/autcmsDatasource
      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(Unknown Source)
           at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
           at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
           at javax.naming.InitialContext.lookup(Unknown Source)
           at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
           at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
           at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
           at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
           at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
           at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
           at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
           at test.NewTest.f(NewTest.java:21)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:607)
           at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
           at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
           at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
           at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
           at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
           at org.testng.TestRunner.runWorkers(TestRunner.java:759)
           at org.testng.TestRunner.privateRun(TestRunner.java:592)
           at org.testng.TestRunner.run(TestRunner.java:486)
           at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
           at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
           at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
           at org.testng.SuiteRunner.run(SuiteRunner.java:204)
           at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:877)
           at org.testng.TestNG.runSuitesLocally(TestNG.java:842)
           at org.testng.TestNG.run(TestNG.java:751)
           at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
           at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
      FAILED: f
      javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not find datasource
           at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)
           at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
           at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
           at test.NewTest.f(NewTest.java:21)
      Caused by: org.hibernate.HibernateException: Could not find datasource
           at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
           at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
           at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
           at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
           at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
           at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
           ... 26 more
      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(Unknown Source)
           at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
           at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
           at javax.naming.InitialContext.lookup(Unknown Source)
           at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
           ... 32 more
      ... Removed 22 stack frames
      


      Please help me.


      Khosro.


        • 2. Re: Testing Seam component and connect to DB in test case class
          Khosro Asgharifard Sharabiani Newbie

          Hi,
          For example in Spring we write a test like this and easily inject Spring bean to it.
          For ex


          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(locations = { "classpath:META-INF/test-context.xml" })
          public class BaseTest {
           @Resource
              AgencyService agencyService;
                  agencyService.findAll();
          }
          
          



          AgencyService.java


          @Transactional
          @Service
          public class AgencyService extends BaseService<Agency> {
           @PersistenceContext
            protected EntityManager em;
          public List<Agency> findAll(){
            Query q=em.createQuery("select a from Agency a");
            return q.getResultList(); 
            }
          }
          


          test-context.xml


              <bean id="mainDs" class="org.apache.commons.dbcp.BasicDataSource" destroy-     method="close" >
                  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                  <property name="url" value="jdbc:mysql://localhost:3306/hello?setUnicode=true&amp;characterEncoding=UTF-8"/>
                  <property name="username" value="root"/>
                  <property name="password" value="123456"/>
              </bean>
          
              <aop:aspectj-autoproxy proxy-target-class="true" />
              
              <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                    p:persistenceUnitName="defaultpu">
                  <property name="dataSource" ref="mainDs"/>
                  <property name="jpaVendorAdapter">
                      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                          <property name="showSql" value="false"/>
                          <property name="generateDdl" value="true"/>
                          <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                      </bean>
                  </property>
                </bean>
          
              <!-- TX Config -->
              <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
                    p:entityManagerFactory-ref="entityManagerFactory" />
              <tx:annotation-driven transaction-manager="transactionManager" />
          




          You can find something like this in this link http://blog.zenika.com/index.php?post/2010/02/05/Testing-SQL-queries-with-Spring-and-DbUnit%2C-part-2


          Is it possible do the same with Seam?


          Khosro.


          • 3. Re: Testing Seam component and connect to DB in test case class
            Khosro Asgharifard Sharabiani Newbie

            Khosro Asgharifard wrote on Feb 16, 2010 20:06:


            Hi,
            For example in Spring we write a test like this and easily inject Spring bean to it.
            For ex

            @RunWith(SpringJUnit4ClassRunner.class)
            @ContextConfiguration(locations = { "classpath:META-INF/test-context.xml" })
            public class BaseTest {
             @Resource
                AgencyService agencyService;
            
                @Test  
               public void test(){
                    agencyService.findAll();
                }
            }
            
            



            AgencyService.java

            @Transactional
            @Service
            public class AgencyService extends BaseService<Agency> {
             @PersistenceContext
              protected EntityManager em;
            public List<Agency> findAll(){
              Query q=em.createQuery("select a from Agency a");
              return q.getResultList(); 
              }
            }
            


            test-context.xml

                <bean id="mainDs" class="org.apache.commons.dbcp.BasicDataSource" destroy-     method="close" >
                    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/hello?setUnicode=true&amp;characterEncoding=UTF-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </bean>
            
                <aop:aspectj-autoproxy proxy-target-class="true" />
                
                <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                      p:persistenceUnitName="defaultpu">
                    <property name="dataSource" ref="mainDs"/>
                    <property name="jpaVendorAdapter">
                        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                            <property name="showSql" value="false"/>
                            <property name="generateDdl" value="true"/>
                            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                        </bean>
                    </property>
                  </bean>
            
                <!-- TX Config -->
                <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
                      p:entityManagerFactory-ref="entityManagerFactory" />
                <tx:annotation-driven transaction-manager="transactionManager" />
            




            You can find something like this in this link http://blog.zenika.com/index.php?post/2010/02/05/Testing-SQL-queries-with-Spring-and-DbUnit%2C-part-2

            Is it possible do the same with Seam?

            Khosro.




            Click HELP for text formatting instructions. Then edit this text and check the preview.