Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit/spring properties not loading with application context

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

like image 465
camada Avatar asked Nov 18 '10 13:11

camada


2 Answers

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>
like image 165
Ralph Avatar answered Nov 10 '22 13:11

Ralph


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>
like image 1
Ricardo García Avatar answered Nov 10 '22 15:11

Ricardo García