Switching to non-jta-datasource in persistence.xml..

and not being able to unit-test my code anymore!

This is the class that is responsible for providing EntityManagers to clients in my web application:
package biz.tugay.sakila.core.utils;
 
import javax.persistence.EntityManager;
 
public interface SakilaEntityManagerFactory {
    EntityManager getEntityManager();
    void closeFactory();
}
And here is the implementation:
package biz.tugay.sakila.core.utils;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
public final class SakilaEntityManagerFactoryImpl implements SakilaEntityManagerFactory {
 
    private final EntityManagerFactory entityManagerFactory;
 
    public SakilaEntityManagerFactoryImpl(final String persistenceUnitName) {
        entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
    }
 
    public EntityManager getEntityManager() {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        return entityManager;
    }
 
    public void closeFactory() {
        entityManagerFactory.close();
    }
}
Initially persistence.xml is as seen here:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="SakilaPU" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.connection.provider_class" 
                value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
            <property name="hibernate.connection.characterEncoding" value="utf-8"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.hikari.dataSourceClassName" 
                value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
            <property name="hibernate.hikari.dataSource.url" 
                value="jdbc:mysql://sql7.freemysqlhosting.net:3306/sql7135785"/>
            <property name="hibernate.hikari.dataSource.user" value="sql7135785"/>
            <property name="hibernate.hikari.dataSource.password" value="fyPnz3jxsK"/>
            <property name="hibernate.hikari.maximumPoolSize" value="5"/>
            <property name="hibernate.hikari.dataSource.cachePrepStmts" value="true"/>
            <property name="hibernate.hikari.dataSource.prepStmtCacheSize" value="250"/>
            <property name="hibernate.hikari.dataSource.prepStmtCacheSqlLimit" value="2048"/>
        </properties>
    </persistence-unit>
</persistence>
And everything is working fine.. But then I started itching and decided to use JNDI to obtain the DataSource..

Changing the Configuration
Here is the new persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="SakilaPU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>java:comp/env/jdbc/sakila</non-jta-data-source>
        <class>org.hibernate.hikaricp.internal.HikariCPConnectionProvider</class>
        <properties>
            <property name="connection.useUnicode" value="true"/>
            <property name="connection.characterEncoding" value="utf-8"/>
        </properties>
    </persistence-unit>
</persistence>
I created the file context.xml as seen below, under web-app\META-INF..
<Context>
    <Resource name="jdbc/sakila"
              auth="Container"
              factory="com.zaxxer.hikari.HikariJNDIFactory"
              type="javax.sql.DataSource"
              minimumIdle="5"
              maximumPoolSize="10"
              connectionTimeout="300000"
              dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
              dataSource.user="sql7135785"
              dataSource.password="fyPnz3jxsK"
              dataSource.url="jdbc:mysql://sql7.freemysqlhosting.net:3306/sql7135785"/>
</Context>
And in web.xml added the following element:
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/sakila</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
And everything works just like it was working before..

Do I like the new Approach?

No. Now I have no idea how to unit test my Data Access Objects. The reason is, the following code seen below
public class BaseDaoTest {
 
    private static final String TEST_PERSISTENCE_UNIT = "SakilaPU";
    protected static final SakilaEntityManagerFactory SAKILA_ENTITY_MANAGER_FACTORY;
    static {
        SAKILA_ENTITY_MANAGER_FACTORY = new SakilaEntityManagerFactoryImpl(TEST_PERSISTENCE_UNIT);
    }
}
is not working anymore. JNDI look up in persistence.xml is not working thus we are not getting any DataSources.. And I have no idea how to make JNDI look up work in unit tests with this approach.

The stacktrace clearly states the problem:
java.lang.ExceptionInInitializerError
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:253)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: javax.persistence.PersistenceException: Unable to build entity manager factory
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at biz.tugay.sakila.core.utils.SakilaEntityManagerFactoryImpl.<init>(SakilaEntityManagerFactoryImpl.java:17)
    at biz.tugay.core.dao.BaseDaoTest.<clinit>(BaseDaoTest.java:20)
    ... 20 more
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    ... 24 more
Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:comp/env/jdbc/sakila]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:95)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)
    ... 35 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:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:499)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118)
    ... 45 more
Do you know how to unit test with the second approach? Please do tell if you do! Thank you!