Ads

Tuesday, September 29, 2009

Devlopement testing using JPA + JUnit

After hours of fight i am able to run Unit tests using JPA

So this is how it goes.

I am using in memory DB
c3p0 is a ComboPooledDataSource more details here
and of course HSQLDB

persistence.xml


<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="TestDB" transaction-type="RESOURCE_LOCAL">

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



<properties>

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



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

<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>

<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:testdb"/>

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

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



<property name="hibernate.c3p0.min_size" value="5"/>

<property name="hibernate.c3p0.max_size" value="20"/>

<property name="hibernate.c3p0.timeout" value="300"/>

<property name="hibernate.c3p0.max_statements" value="50"/>

<property name="hibernate.c3p0.idle_test_period" value="3000"/>



</properties>

</persistence-unit>

</persistence>



Test class




public class MyFirstUnitTest extends TestCase {


EntityManager entityManager;
EntityManagerFactory entityManagerFactory;

public MyFirstUnitTest() {
}

@BeforeClass
public static void setUpClass() throws Exception {
}

@AfterClass
public static void tearDownClass() throws Exception {
}

@Before
public void setUp() {
entityManager =
Persistence.createEntityManagerFactory("TestDB").createEntityManager();

}

@After
public void tearDown() {

entityManager.close();
}

/**
* Test of matches method, of class Format.
*/
@Test
public void testMatches() {

// your test code here


}


}


pom.xml


....
.
.
.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>

.....


you may experience these Exceptions

javax.persistence.PersistenceException: No Persistence provider for EntityManager named
It occures when your Persistence.xml is not in classpath or your Persistence-unit name is not matching with the "TestDB"
Persistence.createEntityManagerFactory("TestDB1")...


javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist

If you try to pass

entityManager.persist("test");
where as test contains setId and you might have taken Id as
@Id
@GeneratedValue
remove setId and try again

Cheerssssss ;)

No comments:

Post a Comment