How to test a DAO (using Spring and JPA) outside a container?

1

I'm developing an application with JPA 2, Spring and JSF 2 running in WildFly 8.0.0. I have already developed some previous applications following the same specification but never tested properly using jUnit. Now, I would like to change that and test more.

As I am very early in development and already want to test the application, came the first problem. How do I test my application with it running out of a container ? I say this because the datasource of my application is managed by the container. So, being outside of it I do not have a datasource .

I thought of assigning the responsibility of managing the datasource for Spring when I'm running the tests, but that implies new configuration files when I'm running the tests. More configuration files means more future maintenance. I would like to avoid this scenario, but if there is no other way ...

Concluding and leaving the question, is there any way to unitarily test the application outside of a container with respect to datasource access?

EDIT: Adding some information that may be needed for the response:

My datasource ( minhaapp-ds.xml ):

<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
    <datasource jta="false" jndi-name="java:/datasource/minhaappds" pool-name="minhaAppDS" enabled="true" use-ccm="false">
        <connection-url>jdbc:oracle:thin:@etc:0000</connection-url>
        <driver-class>oracle.jdbc.OracleDriver</driver-class>
        <driver>ojdbc6.jar</driver>
        <pool>
            <min-pool-size>1</min-pool-size>
            <max-pool-size>15</max-pool-size>
        </pool>
        <security>
            <user-name>minha_app</user-name>
            <password>minha_app</password>
        </security>
        <validation>
            <validate-on-match>false</validate-on-match>
            <background-validation>false</background-validation>
        </validation>
        <timeout>
            <idle-timeout-minutes>5</idle-timeout-minutes>
        </timeout>
        <statement>
            <share-prepared-statements>false</share-prepared-statements>
        </statement>
    </datasource>
</datasources>

My persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="minhaAppPU" transaction-type="JTA">
        <jta-data-source>java:/datasource/minhaappds</jta-data-source>

        <class>br.com.cpy.model.Classe1</class>
        <class>br.com.cpy.model.Classe2</class>
        <class>br.com.cpy.model.Classe3</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/minhaappEMF" />
        </properties>
    </persistence-unit>
</persistence>

And finally, spring-persistence-context.xml

<jee:jndi-lookup jndi-name="java:jboss/minhaappEMF" id="entityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" />
<tx:jta-transaction-manager />
<tx:annotation-driven />
    
asked by anonymous 31.03.2014 / 20:26

1 answer

3

The only way to do this is to do a JNDI MOCK, as it was posted on Oracle's Randy Carver Blog.

 @BeforeClass
public static void setUpClass() throws Exception {
    // rcarver - setup the jndi context and the datasource
    try {
        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, 
            "org.apache.naming");            
        InitialContext ic = new InitialContext();

        ic.createSubcontext("java:");
        ic.createSubcontext("java:/datasource");

        // Construct DataSource
        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setURL("jdbc:oracle:thin:@host:port:db");
        ds.setUser("MY_USER_NAME");
        ds.setPassword("MY_USER_PASSWORD");

        ic.bind("java:/datasource/minhaappds", ds);
    } catch (NamingException ex) {
        Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex);
    }

You can read his explanation better at link

Another important point is the unit test concept. This is covered very well in a unit test response with EJB, it is a valid read. link

    
25.04.2014 / 19:53