lundi 26 janvier 2015

Using H2 Database In-Memory does not persist Objects in Unit Tests

I have a problem concerning JPA in persistence.xml for unit tests. While persisting objects to the database H2 in-memory an error occurs:



-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running some.class.MyClass
################################################################################
BeforeClass Start
################################################################################
EntityManager is being created..
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.541 sec <<< FAILURE!

Results :

Tests in error:
some.class.MyClass: getSingleResult() did not retrieve any entities


I am sure there's a problem with persisting, NOT with obtaining the data, because reading methods in DEV application work perfectly with real databases.


Here is my persistence.xml:



<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://ift.tt/1cKbVbQ" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/1cKbVbQ http://ift.tt/1kMb4sd">
<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>all entities</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

<properties>
<property name="eclipselink.logging.level" value="SEVERE"/>
<!-- ALL SEVERE -->
<property name="eclipselink.logging.level.sql" value="SEVERE"/>
<!-- ALL SEVERE -->
<property name="eclipselink.logging.parameters" value="true"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=Oracle;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS DEV"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<!-- model creation have to be done via javax.persistence, not hibernate/eclipselink - otherwise import doesn't works -->
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
</properties>

</persistence-unit>
</persistence>


</persistence-unit>


And here's a Java piece of code:



@BeforeClass
public static void setUpClass() {
myOut("BeforeClass Start");
EM = Persistence.createEntityManagerFactory(PU).createEntityManager();
myOut("EntityManager is being created..");
EM.persist(new Account(PU, PU, PU));
EM.persist(new Account(PU + "2", "dsaddsa", "dsadas"));
EM.persist(new Account(PU + "3", "saddsd", "dsadasa"));
EM.persist(new Account(PU + "4", "adsada", "hdsd"));

final DaoLogged dao = new DaoLogged();
dao.setEntityManager(EM);
dao.getAccountByLogin(PU);
myOut("BeforeClass Done.");
}


I will appreciate any help! :) Regards!


Aucun commentaire:

Enregistrer un commentaire