Rulz: EntityManager Provider for Integration Tests

I used EntityManager initialization for an integration test as a possible use case for JUnit Rule during the recent Continuous Java EE 7 Testing, Deployment and Code Quality workshop:



import static com.airhacks.rulz.em.EntityManagerProvider.persistenceUnit;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import static org.junit.Assert.assertNotNull;
import org.junit.Rule;
import org.junit.Test;


public class WorkshopTest {

    @Rule
    public EntityManagerProvider emProvider = persistenceUnit("it");

    @Test
    public void crud() {
        EntityManager em = emProvider.em();
        assertNotNull(em);
        EntityTransaction tx = emProvider.tx();
        assertNotNull(tx);
        tx.begin();
        em.merge(new Workshop("Testing", "test what matters));
        tx.commit();
    }
}

I noticed, the code is reusable, so I extracted the rule and created the open source project: Rulz and pushed the library into maven central:


<dependency>
    <groupId>com.airhacks.rulz</groupId>
    <artifactId>em</artifactId>
	<version>[RECENT_VERSION]</version>
    <scope>test</scope>
</dependency>

A JUnit Rule is an alternative to inheritance, which I used so far: BCE Archetype.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting

Comments:

Needle4J is a very nice project for unit testing JavaEE components. With automatic creation and injection of mocks and setup of an in-memory database incl. Entity Manager.

See:
https://github.com/needle4j/needle4j

Posted by Carsten Erker on February 26, 2015 at 10:43 PM CET #

Hello,

I tried this example with 2 integration test classes and all the time I get this error for the first @Rule annotation:

Caused by: Exception [EclipseLink-23001] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.TransactionException
Exception Description: Error looking up external Transaction resource under JNDI name [java:appserver/TransactionManager]
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.exceptions.TransactionException.jndiLookupException(TransactionException.java:47)
at org.eclipse.persistence.transaction.AbstractTransactionController.jndiLookup(AbstractTransactionController.java:454)
at org.eclipse.persistence.transaction.glassfish.GlassfishTransactionController.acquireTransactionManager(GlassfishTransactionController.java:44)
at org.eclipse.persistence.transaction.JTATransactionController.<init>(JTATransactionController.java:67)
... 53 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.eclipse.persistence.transaction.AbstractTransactionController.jndiLookup(AbstractTransactionController.java:452)

Am I doing something wrong?
I'm using junit 4.12. Thanks!

Posted by Madalina on August 05, 2016 at 02:12 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License