hammerfest
hammerfest

Reputation: 2273

Either application or integration test Spring context creation fails due to properties file location type

Our application should use a .properties file which is created in deploy time on the host filesystem, thus it is not being bundled with the application .war in build time. It is opened and read by a loader class during the Spring application context creation

<bean id="propertiesLoader" class="com....PropertiesLoader">
  <property name="propertiesFileLocation" value="${PROPERTIES_FILE_LOCATION}"/>
  ...

where the file path value of PROPERTIES_FILE_LOCATION is defined in theapplication.properties file in src\main\resources of the application's -web maven module

PROPERTIES_FILE_LOCATION=/home/.../propertyfilename.properties

and the type of the propertiesFileLocation field is String and it is being accessed via new FileInputStream(propertiesFileLocation) in PropertiesLoader.java class

With this setup the application context creation is successful. However, we also have some integration tests in our project which also setup a Spring context for their execution. We though to store a copy of propertyfilename.properties file into src\test\resources of the -it maven module and refer it from its it.properties like

PROPERTIES_FILE_LOCATION=classpath:somedirectory/propertyfilename.properties    

however during the creation of the integration test context we receive the following exception

[ERROR] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'propertiesLoader' defined in URL 
[ERROR] Caused by: java.io.FileNotFoundException: classpath:somedirectory\propertyfilename.properties  
(The filename, directory name, or volume label syntax is incorrect)

Now if we modify the type of the propertiesFileLocation field to org.springframework.core.io.Resource and we access it via propertiesFileLocation.getInputStream() in PropertiesLoader.java, that makes the integration tests context creation succesful, but at the same the real application context creation fails with

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertiesLoader' defined in URL 
[file:/.../tomcat/work/Catalina/localhost/_/loader/conf/spring/springmvc/springmvc-util.xml]: Invocation of init method failed; 
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/home/.../propertyfilename.properties]

This seems to be a "deadlock" situation, as either the application or the integration test context creation fails. Is there a way with which both context can be succesfully created?

Upvotes: 0

Views: 155

Answers (1)

M. Deinum
M. Deinum

Reputation: 124526

Resource loading is quite well documenten in the Spring References Guide. When you don’t prefix the resources with the location to load it from it will default to the default location of the ApplicationContext. For the web application that will be the root of the archive, basically what is in the WEB-INF directory. \

You want to load a file resource for this prefix the resource with file: and in your test use classpath: to have it loaded from the `classpath. This way it will work regardless of which default location the application context uses.

Upvotes: 1

Related Questions