【问题标题】:junit/spring properties not loading with application contextjunit/spring 属性未与应用程序上下文一起加载
【发布时间】:2011-05-12 01:26:51
【问题描述】:

在运行 junit 测试时,我无法获取应用程序上下文以从外部属性文件加载属性。

鉴于以下情况:

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/app-config.xml")
public class JdbcWatsonDaoTests {

    @Autowired
    JdbMyDao jdbcMyDao;

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testMethod() {
        doSomeStuff();
    }
}

app-config.xml

<util:properties id="aProperties" location="classpath:spring/a.properties" />
<util:properties id="bProperties" location="classpath:spring/b.properties" />

<bean id="oracleDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="${oracle.url}"/>
    <property name="username" value="${oracle.username}"/>
    <property name="password" value="${oracle.password}"/>
</bean>

a.properties 和 b.properties 文件与 app-config.xml 位于同一位置...

我发现在运行测试时,属性占位符(文字 "${property}" )是发送到 oracle 服务器的内容,而不是属性文件中的值。

我也尝试过使用 PropertyPlaceholderConfigurer 而不是 bean 配置,但它仍然找不到/包含属性。

我正在使用 eclipse helios、spring 3.0.5、最新版本 m2eclipse 和 4.4 junit。我不得不为不同的 maven/junit 错误降级 junit。

在 tomcat 中发布时,会读取并正确使用属性。我只在运行 junit 测试时才看到问题。

【问题讨论】:

  • 您是通过 maven 构建还是通过 eclipse 运行测试 > run as > junit test?
  • eclipse>run as>junit test maven 此时仅管理依赖项。我之所以提出它,是因为我遇到了另一个与构建路径顺序有关的错误。

标签: java eclipse spring junit maven


【解决方案1】:

看来您正在使用 Maven。这将有助于了解您将文件放在哪里。按照惯例,属性文件的测试版本应该放在 src/test/resources/ 中,生产版本应该放在 src/main/resources 中。它们应该会自动解决。

【讨论】:

  • src/main/resources/spring/app-config.xml a.properties b.properties 我没有将它们复制到 src/test/resources。我只是复制它们,清理并重新运行测试,它仍然无法正常工作。实际抛出的异常是:org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;嵌套异常是 org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; login denied 如果我对 xml 文件中的值进行硬编码,它可以工作,但仍然不会读取道具文件。
【解决方案2】:

根据您的例外情况:

org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获得 JDBC 连接;嵌套的 例外是 org.apache.commons.dbcp.SQLNestedException: 不能创造 PoolableConnectionFactory (ORA-01017: 无效的用户名/密码;登录 拒绝

您的问题不是找不到属性,如果找不到属性,异常将类似于org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'

这是因为 您需要配置一个 PropertyPlaceholderConfigurer 而不是 PropertiesFactoryBean(这是 util:properties 所做的http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#xsd-config-body-schemas-util-properties

<bean id="propertyPlaceHolderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:spring/a.properties</value>
                <value>classpath:spring/a.properties</value>
            </list>
        </property>
    </bean>

【讨论】:

  • 但是当我用实际的 url/用户名/密码值替换应用程序上下文中的占位符文本时,测试运行。我唯一能想到的是没有正确读取或找到这些属性。 [info] 日志甚至声明属性已加载。当它发布到 Web 容器时,一切正常。即正确地找到、读取和插入属性。 :(
  • 测试使用真实值运行,因为它们被读取并分配为字符串。已找到属性但未翻译它们,即“${foo.bar}”将被视为字符串而不是某些值的键。也许 Web 容器部署涉及完整上下文中的 PlaceholderConfigurer。在您的测试中,您只有一个最小的 ContextConfig。
  • @Ralph 我试图从第一条评论中回答 camada 的问题(至少对我来说似乎是一个问题)。但是忘记给他加标签了
【解决方案3】:

我放弃了。我下载了 Eclipse 3.6 for Java EE 的新副本,并通过更新站点方法按照 springsource 工具套件安装说明进行操作。

我将我的项目导入到具有新工作区的新环境中,并且一切正常。

我会把它归结为 eclipse gnome。

【讨论】:

  • 放弃和重做是最后要做的事情。这不是问题的解决方案。
【解决方案4】:

您可以将您的测试配置文件、spring 上下文、jbdc.properties 分离到 src/test/resources 目录中以尊重 maven 结构文件。 要为测试配置特殊的 properties 文件,您必须使用 propertyPlaceHolderConfigurer 在测试 spring 应用程序上下文中定义它们,如 Ralph 所说。

属性文件必须在 src/test/resources 中并使用斜杠和文件名 /a.properties 加载它们。将该文件与spring配置文件放在同一目录下即可加载。

<bean id="propertyPlaceHolderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/a.properties</value>
            <value>/a.properties</value>
        </list>
    </property>
</bean>

【讨论】:

    猜你喜欢
    • 2015-04-27
    • 2013-02-03
    • 2018-07-11
    • 1970-01-01
    • 2012-05-24
    • 2012-08-03
    • 2021-06-08
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多