2 Replies Latest reply on Jul 4, 2011 6:17 AM by hibernator_11

    Testing persistence Resource Local in Junit in JBoss 6

    hibernator_11

      Hi all,

       

      I would like to run some Junit tests in my project in JBoss 6 with my Resouce Local Persistence Unit.

       

      {code:xml}

      ...

      <persistence-unit name="db-test" transaction-type="RESOURCE_LOCAL">

              <provider>org.hibernate.ejb.HibernatePersistence</provider>

              <class>com.cervantesvirtual.entity.Car</class>

                ...

              <properties>

                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />

                  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />

                  <property name="hibernate.connection.username" value="xxxx" />

                  <property name="hibernate.connection.password" value="xxxx" />

                  <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/dbtest" />

                  <property name="hibernate.hbm2ddl.auto" value="create" />

                  <property name="hibernate.show_sql" value="true" />

                  <property name="hibernate.search.default.directory_provider" value="filesystem" />

                  <property name="hibernate.search.default.indexBase" value="/var/lucene/testindexes" />

              </properties>

          </persistence-unit>

      ...

      {code}

       

      And here is my junit test:

       

      {code}

      public class PersistenceUnitTest

      {

         

          static private final String PERSISTENCE_UNIT_NAME = "db-test";

          private EntityManager entityManager;

         

          @Before

          public void setUp()

          {

              entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();

          }

       

          @After

          public void tearDown()

          {

              entityManager.close();

          }

       

          @Test

          public void testPersistence()

          {

                ....

          }

      }

      {code}

       

      When i try to run the test, i got an exception:

       

      java.lang.RuntimeException: You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named...

       

      Then, I defined a non-jta-data-source in jboss and I add <non-jta-data-source>java:/TestDS</non-jta-data-source> to my persistence.xml. First question, why would i need to create this in jboss if i only want to run the test?

       

      Then i had more problems with jndi...i had to add jndi.properties...i now i don't really know what i have to do..

       

      can anybody tell me how to run a test with a resource local persistence?

       

      thanks.