【问题标题】:Spring JUnit ClassPathResource FileNotFoundExceptionSpring JUnit ClassPathResource FileNotFoundException
【发布时间】:2015-11-02 08:41:17
【问题描述】:

我正在春季迈出第一步。在我的项目中,我需要使用 SOAP HTTPS 网络服务。我添加了 JKSKeyManager bean 来测试配置 xml 和带有证书的 ClassPathResource bean。但是当我运行 JUnit 测试时,我得到了

java.io.FileNotFoundException:类路径资源 [src/main/resources/cacerts] 无法解析为 URL,因为它不存在

但是文件肯定在那个目录中。

下面是我的配置 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>

我做错了什么?

【问题讨论】:

    标签: java spring maven junit filenotfoundexception


    【解决方案1】:

    从您的项目结构来看,您可能使用的是 Maven,这意味着src/main/resources 是一个源文件夹。

    因此,您需要将配置更改为:

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

    类路径资源的路径不是相对于项目根目录,而是相对于项目源文件夹。

    【讨论】:

    • 谢谢。当然,我应该提到我使用过 Maven
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2016-09-08
    • 2012-03-02
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多