Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playframework 2.2.x + Java +JPA - Separate Database for Unit Testing and Production

I am using Java + Playframework + JPA using PlayFramework 2.3-M1 for developing application.

I want to use MySQL database when I run the aplication, but when I run test using

activator test

I want to use in memory database H2.

How can this be done ?

Below is my

conf/META-INF/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_2_0.xsd"
             version="2.0">

    <persistence-unit name="prod" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>prodDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" 
                      value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"></property>
            <property name="hibernate.format_sql" value="true"></property>
        </properties>
    </persistence-unit>

    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>TestDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" 
                      value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"></property>
            <property name="hibernate.format_sql" value="true"></property>
        </properties>
    </persistence-unit>

</persistence>

conf/application.conf

# Database configuration
# ~~~~~ 
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.test.driver=org.h2.Driver
db.test.url="jdbc:h2:mem:play"
db.test.user=sa
db.test.password=""

db.test.jndiName=TestDS

jpa.test=testPersistenceUnit

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://X.X.X.X/lb"
db.default.user=lb
db.default.password="lb"

#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS


jpa.default=defaultPersistenceUnit


# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled

# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"

Is this the best approach to get it done?

Thanks

like image 724
Reg Mem Avatar asked Apr 25 '14 16:04

Reg Mem


1 Answers

So I used some mix/match approach for faster testing

  1. I used separate configuration files for testing and production
  2. I used two different JPA Persistence Units for Prod and Testing
  3. Added overrode the config file for test execution in build.sbt
  4. Utilized to generate test schema
  5. Utilized evolution to load test data fixtures into test data

Below are my files:

conf/test.conf

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:lb"
db.default.user=lb
db.default.password=""

#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS


jpa.default=testPersistenceUnit


# Evolutions
# ~~~~~
# You can disable evolutions if needed
#evolutionplugin=disabled

conf/application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://X.X.X.X/lb"
db.default.user=lb
db.default.password="lb"

#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS

jpa.default=defaultPersistenceUnit

# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled

Then added following option to build.sbt file

build.sbt

javaOptions in Test += "-Dconfig.file=conf/test.conf"

Finally the persistence.xml file

<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_2_0.xsd"
             version="2.0">

    <!--Persistence Unit for Production-->

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"></property>
            <property name="hibernate.format_sql" value="true"></property>
        </properties>
    </persistence-unit>

    <!--Persistence Unit for Testing-->

    <persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="false"></property>
            <property name="hibernate.format_sql" value="true"></property>
        </properties>
    </persistence-unit>
</persistence>

Finally I test it using

activator test

Thanks to users at play-framework google group for leads into achieving this.

like image 104
Reg Mem Avatar answered Oct 10 '22 14:10

Reg Mem