我正在使用maven build helper自动将integration-test/java
和integration-test/resources
文件夹添加到我的项目classpath
。当我在JUnit
中使用Eclipse
运行我的测试时效果很好,但是当我运行Web项目时遇到问题,其中Tomcat总是选择测试资源而不是生产测试资源。
我的项目结构是这样的:
DaoProject
src/main/resources/datasource.properties
src/main/resources/spring-data.xml
src/main/java/....
WebProject
src/main/resources/appContext.xml
src/integration-test/java/com/mypkg/controller/MyControllerIT.java
src/integration-test/resources/datasource.properties
appContext.xml
导入spring-data.xml
,然后datasource.properties
加载<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config />
<tx:annotation-driven/>
<context:component-scan base-package="com.mypkg" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:defaultEncoding="UTF-8">
<property name="basenames">
<array>
<value>messages</value>
</array>
</property>
</bean>
<import resource="classpath:datasource.xml" />
作为来源占位符。
appContext.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:property-placeholder location="classpath:datasources.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${config.db.url}" />
<property name="username" value="${config.db.username}" />
<property name="password" value="${config.db.password}" />
datasource.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/integration-test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
pom.xml:
{{1}}
答案 0 :(得分:0)
我认为问题在于您加载datasource.properties
文件的方式。尝试在PropertyPlaceholderConfigurer
:
<context:property-placeholder location="classpath:src/main/resources/datasources.properties"/>
希望这有帮助。