开发者

junit/spring properties not loading with application context

开发者 https://www.devze.com 2023-01-25 13:50 出处:网络
While running a junit test, I cannot get the application context to load properties from external properties files.

While running a junit test, I cannot get the application context to load properties from external properties files.

Given the following:

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>

and the a.properties and b.properties files are in the same location as app-config.xml...

I've found that when runni开发者_开发问答ng the test, the properties placeholders (the literal "${property}" )are what is being sent to the oracle server instead of the values in the properties files.

I've also tried using a bean configuration using PropertyPlaceholderConfigurer instead of , but it still doesn't find/include properties.

I am using eclipse helios, spring 3.0.5, newest release m2eclipse and 4.4 junit. I had to downgrade junit for a different maven/junit bug.

When published within tomcat, the properties are read and properly used. I only see the problem when running a junit test.


According to your exception:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied

Your probelm is NOT that the properties are not found, if the properties are not found the exception would be something like org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'

And this is because you need to configure a PropertyPlaceholderConfigurer instead of a PropertiesFactoryBean (this is what util:properties does 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>


You may separate your test configuration files, spring context, jbdc.properties, into src/test/resources dir to respect maven structure files. To configure special properties files for test you have to define them in test spring application context using propertyPlaceHolderConfigurer as Ralph said.

Property files must be in src/test/resources and load them with the slash and file name /a.properties. Place the file in the same directory as the spring configuration file to load it.

<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>


It appears you're using maven. It would help to know where you put the files. By convention, the test version of the properties files should go in src/test/resources/ and production version in src/main/resources. They should resolve automatically.


I gave up. I downloaded a fresh copy of Eclipse 3.6 for Java EE and followed the springsource tool suite installation instuctions via the update site method.

I imported my project to the new environment with a new workspace and everythign works just fine.

I'll chalk it up to eclipse gnomes.

0

精彩评论

暂无评论...
验证码 换一张
取 消