Sergey Maksimenko
Sergey Maksimenko

Reputation: 588

Spring JUnit ClassPathResource FileNotFoundException

I'm making my first steps in Spring. In my project I need to consume SOAP HTTPS web-service. I've a added JKSKeyManager bean to test config xml and ClassPathResource bean with certs. But when I run JUnit test I get

java.io.FileNotFoundException: class path resource [src/main/resources/cacerts] cannot be resolved to URL because it does not exist

but file is definitely in that directory.

Below is my config xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">`
<bean id="cucmsql" class="com.myname.myproject.CUCMDatabaseConnector" factory-method="getInstance">
    <property name="marshaller" ref="marshaller"></property>
    <property name="unmarshaller" ref="marshaller"></property>
    <property name="properties" ref="properties"/>
</bean>
<bean id="properties" class="com.myname.myproject.SystemProperties" factory-method="getInstance">
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.cisco.axl" />
 </bean>
 <bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="src/main/resources/cacerts"/>
 </bean>
 <util:property-path id="cacertsUtil" path="cacertsFile.file"/>`
 <bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
    <constructor-arg ref="cacertsUtil"></constructor-arg>
    <constructor-arg>
        <map>
            <entry key="cucmpublisher" value="changeit"></entry>
            <entry key="cucmsubcriber" value="changeit"></entry>
        </map>
    </constructor-arg>
    <constructor-arg type="java.lang.String" value="changeit"></constructor-arg>
 </bean>
</beans>

What am I doing wrong?

Upvotes: 0

Views: 1856

Answers (1)

Tunaki
Tunaki

Reputation: 137064

Judging by your project structure, you are probably using Maven, which means src/main/resources is a source folder.

As such, you need to change your configuration to:

<bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="/cacerts" />
</bean>

The path to a classpath resource is not relative to the project root but to the project source folders.

Upvotes: 1

Related Questions