Junit / spring properties do not load with application context

When running the junit test, I cannot force the application context to load properties from external property 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 place as app-config.xml ...

I found that when I run the test, the placeholders properties (literal "$ {property}") are what is sent to the oracle server instead of the values ​​in the properties files.

I also tried using the bean configuration using PropertyPlaceholderConfigurer instead, but it still cannot find / enable properties.

I am using eclipse helios, spring 3.0.5, the latest release of m2eclipse and 4.4 junit. I had to lower junit for another maven / junit error.

When published to tomcat, properties are read and used correctly. I see the problem only when running the junit test.

+6
java spring eclipse maven junit
source share
4 answers

According to your exception:

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to get JDBC connection; nested org.apache.commons.dbcp.SQLNestedException exception: Cannot create PoolableConnectionFactory (ORA-01017: Invalid username / password; login denied

Your task is not that the properties are not found, if the properties are not found, the exception will look like org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'

And this is because you need to set PropertyPlaceholderConfigurer instead of PropertiesFactoryBean (this is what uses: 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> 
+5
source share

You can separate your test configuration files, spring context, jbdc.properties, in the src / test / resources directory to respect the maven structure files. To set up special properties files for testing, you must define them in the test context of spring using propertyPlaceHolderConfigurer as Ralph .

Property files must be located in src / test / resources and loaded with a slash and the /a.properties file name . Place the file in the same directory as the spring configuration file to download 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> 
+1
source share

You seem to be using maven. This will help you find out where you put the files. By convention, the test version of the property files should go to src / test / resources / and the production version to src / main / resources. They should be automatically resolved.

0
source share

I gave up. I downloaded a new copy of Eclipse 3.6 for Java EE and monitored the installation of the springsource tool installation through the update site method.

I imported my project into a new environment with a new workspace, and everything works fine.

I will do this to overshadow the gnomes.

0
source share

All Articles