Unit Test with TestNG - Could not find datasource
charliebarjel Dec 11, 2009 8:15 AMHi,
Im not sure if I am using testNG to run my unit tests the right way, so I just need a few pointers.
I've written a small Unit Test to insert a few records into my database based on the example from the Seam Reference - http://docs.jboss.org/seam/2.0.0.GA/reference/en/html/testing.html.
However, when I run it using the ANT script generated via seam-gen, I get the error Could not find datasource
Is this the correct way to run the Unit tests?
Here are my files:
build.xml - test target
<target name="test" depends="buildtest" description="Run the tests"> <fail message="Cannot run tests because path to project contains spaces."> <condition> <contains string="${basedir}" substring=" "/> </condition> </fail> <condition property="incompatible.jdk" value="true"> <and> <equals arg1="${ant.java.version}" arg2="1.6"/> <not><available classname="javax.xml.bind.JAXB"/></not> </and> </condition> <fail if="incompatible.jdk">You are using an incompatible JDK 6. Please use Sun JDK 6 Update 4 (1.6.0_04) or newer or use Open JDK 6.</fail> <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"> <jvmarg line="-Dsun.lang.ClassLoader.allowArraySyntax=true"/> <classpath refid="test.path" /> <xmlfileset dir="${test.dir}" includes="testng.xml" /> </testng> </target>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Persistence deployment descriptor for test profile --> <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="koncentrator" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>koncentratorDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> <property name="hibernate.hbm2ddl.auto" value="validate"/> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.default_schema" value="CAIROTEST"/> <!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) --> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> </properties> </persistence-unit> </persistence>
ActivityLogTest.java
package au.com.statewater.koncentrator.test; import java.sql.Date; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.testng.annotations.Test; import au.com.statewater.koncentrator.entity.ActivityLog; public class ActivityLogTest { private EntityManagerFactory emf; @Test public void testActivityLog() { emf = Persistence.createEntityManagerFactory("koncentrator"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); ActivityLog activity = new ActivityLog(); activity.setEvent("UPDATE"); activity.setUsername("cbarjel"); activity.setDescription("Updated the valley"); activity.setActivityDate(new Date(new java.util.Date().getTime())); em.persist(activity); // assert "success".equals( em.persist(activity) ); assert true; em.getTransaction().commit(); em.close(); } }
Do I need to copy my datasource file anywhere as well?
Thanks for your help.
Charlie