Abhijat M

Abhijat M

Thursday, 31 January 2013

Junit Tests OpenJPA and Websphere EJB3.0 + DB2 9.0

Junit Tests OpenJPA and Websphere EJB3.0 DB2

For those of us with IBM and their beau software aka tools.

Heres what i had to do to configure the websphere Enterprise container to fire JUnits to persist EJB’s over OpenJPA 1.2.3

You will have to Override persistence.xml file settings to start using JDBC over JTA.

Here’s a snippet from my Abstract Test Case class
Most interesting one is the TransactionMode which is correlated to using

  configOverrides.put("openjpa.TransactionMode", "local");

Allows you to reuse a single persistence unit as Websphere has a bug wherein it would not allow multiple persistence Units to load together. Even worse a separate one for test also fails

Just one more thing
Remember to commit or close connections in the end so:

 public void tearDown() throws Exception {
Complete Abrstract Template
public abstract class AbstractTestCase extends TestCase {

 private Logger logger = LoggerFactory.getLogger(AbstractTestCase.class);
 private EntityManagerFactory emf;

 protected EntityManager em;
 protected EntityTransaction entityTransaction = null;
 public static void setUpBeforeClass() throws Exception {

  * @throws java.lang.Exception
 public static void tearDownAfterClass() throws Exception {

  * @throws java.lang.Exception
  * Finally close connection
  * ensures always
 public void setUp() throws Exception{
  logger.debug("Initialise Setup");
  Properties prop = new Properties();
     try {
               //load a properties file
     } catch (IOException ex) {

  logger.debug("Loading properties file from TestSuite.properties");

  Map configOverrides = new HashMap();
  configOverrides.put("openjpa.jdbc.Schema", prop.getProperty("jdbc.schemaname"));    
  configOverrides.put("openjpa.ConnectionURL", prop.getProperty("jdbc.connectionurl"));
  configOverrides.put("openjpa.ConnectionDriverName", prop.getProperty("jdbc.connectionDriverName"));
  configOverrides.put("openjpa.ConnectionUserName", prop.getProperty("jdbc.username"));
  configOverrides.put("openjpa.ConnectionPassword", prop.getProperty("jdbc.password"));
  configOverrides.put("openjpa.TransactionMode", "local");
  logger.debug("Finished load of properties file from TestSuite.properties");

     emf = Persistence.createEntityManagerFactory("Entity", configOverrides);
     em = emf.createEntityManager();
     entityTransaction = em.getTransaction();
  * @throws java.lang.Exception
 public void tearDown() throws Exception {
  logger.debug("Initialise tearDown");
  logger.debug("All connections closed Successfully");