Failed to load properties from junit spring - spring

I have got a project which works perfectly fine when run via java -jar command but if I run it via junit it fails not able to load property files which are loaded using spring context.Below is the directory structure
/src
/main
/java
/resources
/config
/export
export.properties
context.xml
startJob.bat
test
java
execution
Test1
context.xml contains below:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/export/export.properties</value>
</list>
</property>
My test class is as below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath:context.xml"})
public class Test1 {
#Autowired
Export export;
#org.junit.Test
public void test2() {
export.report();
when I run the above junit test case,
I get below error:
Caused by: java.io.FileNotFoundException: config\export\export.properties (The system cannot find the path specified).
I have even tried coping over all the resources from src/main/resources to src/test/resources but it still fails with the same error.
I believe it is something which everyone would be using and must be pretty straighforward...
EDIT 1
i created a separate test-context.xml and placed it in the sources and modified it to load files using classpath:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/export/export.properties</value>
</list>
</property>.
after this spring is able to load the properties but on further processing I have below code in logger: DOMConfigurator.configure("config/export/export_log.xml") which fails with "java.io.FileNotFoundException: H:\workspaces\export\job\config/export/export_log.xml".how does it pick up this path ?

I was able to resolve the issue by configuring log files as below:
DOMConfigurator.configure(JobLogger.class.getClassLoader().getResource("config/export/export_log.xml"))

Related

Spring Application data source configuration from external file

Is it possible to create a data source by reading the values from an external file which is not bundled with WAR in spring application.
You can use the #PropertySource annotation to load your db properties and you can load the properties from file location like below:-
#PropertySource("file:${app.home}/db.properties")
reference link here:-
https://www.mkyong.com/spring/spring-propertysources-example/
For XML based configuration sample code could be like below:-
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${app.home}/db.properties</value>
</list>
</property>
</bean>
you can setup your datasource in any properties file, and then you have to provide the classpath for that file in your catalina.sh where you are running your war.
don't forget to load that property file in in your application.

Could not resolve placeholder in string value with external files

I have configured a config.xml file choosing the appropriate properties file depending on environment. I am running this as a Spring Boot app with Apache Camel.
The config looks like this.
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations" ref="locations" />
</bean>
<bean id="bridgePropertyPlaceholder"
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="locations" ref="locations" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<beans profile="dev">
<util:list id="locations">
<value>classpath:config/users.properties</value>
<value>classpath:config/application.properties</value>
<value>classpath:config/application-dev.properties</value>
</util:list>
<beans profile="test">
<util:list id="locations">
<value>file:${project.dir}/config/users.properties</value>
<value>file:${project.dir}/config/application.properties</value>
</util:list>
When using the test profile I want to use the external files defined in the config (because I dont want to commit username/password to repo). That seems to work okay.
However, my users.properties file contains:
username=myusername
password=mypassword
and my application.properties contains:
loginParameters=username=${username}&password=${password}
when running java -jar myjar.jar --spring.profiles.active=test I encounter:
java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in string value '${username}&password=${password}'
It clearly loads the properties files because it states:
Loading properties file from URL: file:...../users.properties
Loading properties file from URL: file:...../application.properties
Bridging Camel and Spring property placeholder configurer with id: bridgePropertyPlaceholder
...
And then the exception occurs. How can I resolve the issue where the application.properties file doesn't recognize my properties defined in users.properties? Everything works okay when running the dev-profile.
in your application properties, use $simple{username} and $simple{password} to tell Camel to put the values there.

Using PropertyPlaceholderConfigurer to access test resources

In my IntelliJ project, under the project root, there is a config folder.
This is where various .properties files are located.
Within the application, these are made available to Spring in the standard way, using a propertiesConfigurer.xml file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/${name}.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
The reason they are accessed using 'file', is that eventually they may be external to the deployed jar.
This works, both when running the application, and when running unit tests.
I also wish to access files which are only for use when running tests.
I have placed these under:
test
--- resources
-------META-INF
----------spring
-------------config
I have added this line to the list of locations within the same propertiesConfigurer.xml:
<value>classpath*:META-INF/spring/config/${name}.properties</value>
This is not working. No file or resource exception is being thrown, but none of the properties defined in the test file are being resolved within the application.
Within the IntelliJ Project Settings->Modules, src\test\resources is listed as a context root.
Also, on the file system, under:
...\target\test-classes\META-INF\spring\config
the test property files exist.
What is the correct way to access them?
Thanks

Spring session factory not finding mapping files in multiple directories with same class path

I am having problems when loading hibernate mappings from multiple paths.
My Spring session factory is define as follows:
<beans>
...
<bean id="sessionFactory" class="org.springframwork.orm.hibernate3.LocalSessionFactory">
</bean>
<property name="mappingLocations">
<list>
<value>classpath:/mapping/*.hbm.xml</value>
</list>
</property>
When I put my mappings Foo.hbm.xml and Bar.hbm.xml into the directory src/main/resources/mappings, then both mappings are found when Hibernate is initialized.
But when I put Foo.hbm.xml into the directory src/main/resources/mapping and Bar.hbm.xml into the directory src/test/resources/mapping, then only the latter mapping file can be found. Hibernate will fail with "cannot find mapping for Foo" error.
I can see that the mappings are copied to the directories target/classes/mapping and target/test-classes/mapping, so why cannot hibernate (or the spring local session factory bean) find both mapping files? I thought that "classpath:/mapping/*.hbm.xml" would find both target/classes/mapping and target/test-classes/mapping directories?
edit: I am getting this problem when running unit tests, so I expect that mappings found in both src/main/resources and src/test/resources would be found.
You are using maven. And since you put your Bar mapping into the test resource directory, it is only available when running tests.
I assume you have a persistence-unit configured similar to the example below
<persistence-unit ...>
<class>something.Foo</class>
<class>something.Bar</class>
</persistence-unit>
What happens at start-up is that Spring starts Hibernate, hibernates reads the persistence-unit and asks the factory for the mappings. But remember, Bar is only a test class. So Spring finds the mappings from src/main/resources, but since it does not run as a test, it does not see src/test/resources.

Spring PropertyPlaceholderConfigurer not replacing placeholder

I want to pass the WSDL url for an internal web service into my Spring beans.xml dynamically, using a PropertyPlaceHolderConfigurer.
Here's the scenario:
My web application is deployed in WebLogic 10.3.
The WSDL url is contained in a properties file that is located outside my application (directly under the corresponding domain folder, while my application is inside the autodeploy folder). I set the location of this properties file in my domain's setDomainEnv.cmd file like below:
set JAVA_PROPERTIES=%JAVA_PROPERTIES% %CLUSTER_PROPERTIES% -Dproperty.file.path.config=%DOMAIN_HOME%\Service.properties
This is what my Service.properties file contains:
Service.WSDL.PATH=http://localhost:8088/mockServiceSoap?WSDL
My Spring beans.xml configuration:----
<bean id="file.path" class="java.lang.System" factory-method="getProperty">
<constructor-arg index="0"><value>property.file.path.config</value></constructor-arg>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="file.path"/>
</bean>
<bean id="myServiceId" class="com.test.service.ServiceImpl">
<property name="myServiceSoap">
<ref bean="myService"/>
</property>
</bean>
<bean id="myService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.test.service.ServiceSoap"/>
<property name="wsdlDocumentUrl" value="${Service.WSDL.PATH}"/>
</bean>
I enabled DEBUG log specifically for PPC and this is what I saw in my application log:
INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 178 - Loading properties file from URL [file:D:/bea10.3/user_projects/domains/my_domain/Service.properties]
So, it seems that although the Service.properties file is getting loaded by PPC, the ${Service.WSDL.PATH} is NOT getting replaced.
What am I doing wrong here?
Also, how can I find out if PPC tried replacing the value of the placeholder and with what value? I was hoping the log file would contain that info but there was nothing there.
Any help is appreciated.
I've figured out, that PropertyPlaceholderConfigurer needs to be declared first in the application Context file, otherwise there's no guarantee of the load order. It took me a few hours to realize this.
Try moving the "file.path" bean into the PropertyPlaceHolderConfigurer's location property.

Resources