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

我正在接管现有的Spring / JPA(w / hibernate)/ Maven项目,并试图集成单元测试和集成测试。

该项目的构建和运行良好,但是当我运行Maven Test来运行我的Spring-powered集成测试时,遇到了一个我无法弄清的错误:

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

我有一种预感,它对我的​​POM配置有一些帮助。 我的数据库上下文信息位于一个名为context.xml的文件中,而以前的开发人员似乎已将此文件与tomcat插件绑定在一起? 这是否意味着只能在tomcat上加载数据库信息:运行?

相关的POM内容

<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>

有关我为什么会出现此错误的任何见解?

我也欢迎任何有关如何为此多模块项目(带有子项目的父项目)配置Maven POM的建议或示例代码,以便它可以在每个模块中进行单元测试和集成测试。


context.xml是一个特定于tomcat的配置文件,因此如果您在Web容器之外运行测试,它将不会被使用。 解决方案将是在src/test/resources (如果您遵循标准项目布局)中放置的spring配置文件中为测试定义数据源。 此文件夹仅用于测试类路径,因此它不会干扰您部署的配置。

然后,您的测试将不得不使用这个附加文件初始化一个Spring应用程序上下文。

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

上一篇: Hibernate context initialization error when running Maven Test

下一篇: C# windows server 2008 r2 win32exception