【发布时间】:2014-04-10 18:44:57
【问题描述】:
我的 pom.xml 中有这个:
<build>
<finalName>${project.artifactId}</finalName>
<filters>
<filter>${project.basedir}/src/main/resources/filters/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
...
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>qa</id>
<properties>
<env>qa</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
我在 src/main/resources/filters 中有这些文件:dev.properties、qa.properties 和 prod.properties。
但是我在 src/test/resource 中的 spring config test-db-config.xml 没有获取我在属性文件中的值:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.usename}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
有人能指出我正确的方向吗?谢谢
这是我得到的错误:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.185 sec <<< FAILURE!
testCustomerExists(com.xxxx.hcs.persistence.repository.CustomerRepositoryTest) Time elapsed: 1.973 sec <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
...
在 dev.properties 我有:
db.driver=oracle.jdbc.OracleDriver
db.url=jdbc:oracle:thin:@hrsdev.xxxx.net:1548:yyyy
db.usedb.passwordrname=xxxx
db.password=yyyy
当我将这些值放入 test-db-config.xml 时,测试用例运行良好。所以我知道这是由变量替换引起的。
【问题讨论】:
-
错误信息是什么?还有拼写正确吗? “db.usenamer”
-
您使用什么 Spring 配置来包含属性文件?
-
我打错了。更正了 db.usename 不幸的是,这不是导致错误的原因。
-
请检查此stackoverflow.com/questions/17628528/…。您是否对应用程序和测试使用相同的 appContext?
-
我建议您使用 Spring Profiles 而不是 Maven Profiles 来进行此类操作。它们使在不同环境中工作变得轻而易举,并且不依赖于构建工具
标签: java spring maven spring-mvc