【问题标题】:spring test config ignores maven filter弹簧测试配置忽略 Maven 过滤器
【发布时间】: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


【解决方案1】:

我的直觉是过滤器在过滤操作之前被解析,所以 ${env} 变量还没有被评估并且过滤器永远不会被解析。

您可以通过在 pom 中直接设置全名“dev.properties”来进行测试,这应该会导致有效的过滤。

我建议使用经典方式,直接将您的属性值放入配置文件定义中(请参阅http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering)。

这并不妨碍使用属性文件进行配置,只需有一个包含变量的属性文件(dbConfig.properties),然后让它被 maven 过滤。

[编辑]

似乎最先进的方法是使用propertyConfigurer bean,请参阅https://stackoverflow.com/a/11874827/2087640

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 2015-12-11
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多