7

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.

2
  • Are you running the tests through a maven build or thorugh eclipse > run as > junit test?
    – mR_fr0g
    Nov 18, 2010 at 15:19
  • eclipse>run as>junit test maven at this point is only managing dependencies. I only brought it up because I've encountered another bug with it that had to do with build path order.
    – camada
    Nov 18, 2010 at 15:31

4 Answers 4

6

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>
3
  • But when I replace the placeholder text in the application context with the actual url/username/password values, the test runs. The only thing I can think of is that the properties aren't properly read or found. The [info] logs even state that the properties are loaded. When this is published to a web container, it all works fine. i.e. the properties are found, read and inserted properly. :(
    – camada
    Nov 18, 2010 at 17:07
  • The tests run with real values because they are read and assigned as string. The properties are found but they are not translated, i.e. "${foo.bar}" will be seen as a string not as a key for some value. Maybe the web container deployment involves a PlaceholderConfigurer in the full context. In your test you only have a minimal ContextConfig. May 7, 2019 at 7:47
  • @Ralph I was trying to answer to camada's question (at least seems a question to me) from the first comment. But forgot to tag him May 7, 2019 at 11:18
1

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

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.

1
  • src/main/resources/spring/ app-config.xml a.properties b.properties I did not have them copied to src/test/resources. I just copied them, cleaned and re-ran the test and it still doesn't work. The actual exception thrown is: 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 If I hard code the values in the xml file, it works, but still won't read the prop files.
    – camada
    Nov 18, 2010 at 15:11
-1

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.

1
  • giving up and redoing thing is the last thing to do. This is not a solution to a question. Feb 27, 2021 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.