Unit Test: Could not find datasource: java:DefaultDS
jhedden.jhedden.opsource.net Jul 30, 2008 9:00 PMI 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.