簡體   English   中英

junit / spring屬性未加載應用程序上下文

[英]junit/spring properties not loading with application context

在運行junit測試時,我無法獲取應用程序上下文來從外部屬性文件加載屬性。

鑒於以下內容:

識別TestClass

@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。 我不得不降級junit以獲得不同的maven / junit bug。

在tomcat中發布時,將讀取並正確使用這些屬性。 我只在運行junit測試時看到問題。

根據你的例外情況:

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>

您可以將測試配置文件,spring上下文,jbdc.properties分隔為src / test / resources目錄,以尊重maven結構文件。 要配置用於測試的特殊屬性文件,您必須使用propertyPlaceHolderConfigurer在測試彈簧應用程序上下文中定義它們,如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>

看來你正在使用maven。 知道放置文件的位置會有所幫助。 按照慣例,屬性文件的測試版本應該放在src / test / resources /和src / main / resources中的生產版本中。 他們應該自動解決。

我放棄。 我下載了Eclipse 3.6 for Java EE的全新副本,並通過更新站點方法遵循了泉源工具套件安裝實例。

我將項目導入到具有新工作空間的新環境中,並且每個工作都很好。

我會把它用來遮蔽侏儒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM