1 Reply Latest reply on Dec 17, 2007 11:42 PM by andrew.rw.robinson

    TestNG unit tests with seam 2.0 in JPA environment?

    andrew.rw.robinson

      I am having issues trying to figure out how to configure my unit tests to test my entities and seam components. I am running in the JPA environment. I made a test base class to setup the entity manager. I am just not sure how to hook up the EntityManagerFactory with my datasource.

      Test class code:

      @Override @BeforeClass
       public void init() throws Exception
       {
       super.init();
      
       PGSimpleDataSource dataSource = new PGSimpleDataSource();
       dataSource.setDatabaseName("testdb");
       dataSource.setUser("user");
       dataSource.setPassword("pword");
       dataSource.setServerName("localhost");
      
       getInitialContext().bind("jdbc/testDatasource", dataSource);
       Map<String, String> props = new HashMap<String, String>();
       props.put("hibernate.default_schema", "test");
       props.put("hibernate.default_catalog", "testdb");
      
       entityManagerFactory = Persistence.createEntityManagerFactory("testDatabase", props);
       }
      

      My /src/main/resources/persistence.xml:
      <persistence>
       <persistence-unit name="testDatabase" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:comp/env/jdbc/testDatasource</jta-data-source>
       <properties>
       <property name="hibernate.dialect"
       value="org.hibernate.dialect.PostgreSQLDialect" />
       <property name="connection.release_mode">after_statement</property>
       <property name="hibernate.default_schema">my</property>
       <property name="hibernate.default_catalog">my</property>
       <property name="hibernate.show_sql" value="true"/>
       <property name="hibernate.query.substitutions"/>
       <property name="hibernate.transaction.flush_before_completion" value="true"/>
       <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
       <!--
       <property name="hibernate.transaction.manager_lookup_class"
       value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
       -->
       <property name="jboss.entity.manager.factory.jndi.name"
       value="java:/christwsEntityManagerFactory"/>
       <property name="jboss.entity.manager.jndi.name"
       value="java:/entityManager"/>
       </properties>
       </persistence-unit>
      </persistence>


      Without tomcat to setup the initial context I am not sure how to wire this to get the test running right.

      I am in a maven environment using testng and surefire to run the test.

      Any ideas?

      The error:
      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)
      ...


      Thanks,
      Andrew