Hibernate context initialization error when running Maven Test

I'm taking over an existing Spring/JPA(w/hibernate)/Maven project and attempting to integrate unit testing and integration testing.

The project builds and runs fine, but when I run Maven Test to run my Spring-powered integration test I'm encountering an error I can't figure out:

org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:90)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2836)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2832)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
... 75 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:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
... 82 more

I have a hunch that it has something to with my POM configuration. My database context information is located in a file called context.xml and the previous developer seems to have tied this file to the tomcat plugin? Does this mean the database information only gets loaded on tomcat:run?

Relevant POM content

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <path>/rms-online</path>
                <mode>both</mode>
                <port>80</port>
                <httpsPort>443</httpsPort>
                <scanIntervalSeconds>5</scanIntervalSeconds>
                <contextFile>src/main/resources/context.xml</contextFile>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.16</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
                <webResources>
                    <webResource>
                        <directory>${basedir}/src/main/resources</directory>
                        <includes>
                            <include>context.xml</include>
                        </includes>
                        <targetPath>META-INF</targetPath>
                        <filtering>true</filtering>
                    </webResource>
                    <webResource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                        <targetPath>WEB-INF</targetPath>
                        <filtering>true</filtering>
                    </webResource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="" reloadable="false">
<Resource
    name="jdbc/rmsDS"
    type="javax.sql.DataSource"
    maxActive="50"
    maxIdle="35"
    logAbandoned="true"
    username="xxx"
    password="xxx"
    maxWait="10000"
    validationQuery="select 1"
    driverClassName="com.mysql.jdbc.Driver"
    removeAbandoned="true"
    url="jdbc:mysql://127.0.0.1/rms?zeroDateTimeBehavior=convertToNull"
    removeAbandonedTimeout="300"/>
<Resource
    name="jdbc/rmsDeviceDS"
    type="javax.sql.DataSource"
    maxActive="50"
    maxIdle="35"
    logAbandoned="true"
    username='xxx'
    password='xxx'
    maxWait="10000"
    validationQuery="select 1"
    driverClassName="com.mysql.jdbc.Driver"
    removeAbandoned="true"
    url="jdbc:mysql://127.0.0.1/rms_device_processed?zeroDateTimeBehavior=convertToNull"
    removeAbandonedTimeout="300"/>  
</Context>

Any insights on why I might be getting this error?

I also welcome any suggestions or sample code showing how to configure the Maven POMs for this multi-module project (parent project with child projects) so that it can have unit tests and integration tests in each module.


The context.xml is a tomcat specific configuration file, so if you run your tests outside of the web container it won't be used. The solution would be to define a datasource for tests in a spring configuration file which is placed in src/test/resources (if you followed the standard project layout). This folder is only used for the test classpath and so it won't interfere with your deployed configuraiton.

Your tests would then have to initialize a spring application context using this additional file.

链接地址: http://www.djcxy.com/p/62950.html

上一篇: Spring 3中使用maven的jdbc属性

下一篇: 运行Maven测试时出现Hibernate上下文初始化错误