无法从junit spring加载属性

无法从junit spring加载属性,spring,maven,junit,filenotfoundexception,spring-test,Spring,Maven,Junit,Filenotfoundexception,Spring Test,我有一个项目,它在通过java-jar命令运行时运行得非常好,但是如果我通过junit运行它,它将无法加载使用spring上下文加载的属性文件 /src /main /java /resources /config /export export.properties context.xml startJob.bat test java execution Test1 context.xml包含以下内容: &l

我有一个项目,它在通过java-jar命令运行时运行得非常好,但是如果我通过junit运行它,它将无法加载使用spring上下文加载的属性文件

/src
 /main
  /java
  /resources
    /config
      /export
        export.properties
    context.xml
    startJob.bat
 test
  java
    execution
     Test1
context.xml包含以下内容:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:config/export/export.properties</value>
            </list>
         </property>
当我运行上面的junit测试用例时, 我得到以下错误:

Caused by: java.io.FileNotFoundException: config\export\export.properties (The system cannot find the path specified).
我甚至尝试过处理从src/main/resources到src/test/resources的所有资源,但仍然失败,出现了相同的错误。 我相信这是每个人都会使用的东西,必须非常严格

编辑1 我创建了一个单独的test-context.xml并将其放在源代码中,并对其进行了修改,以使用类路径加载文件:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/export/export.properties</value>
            </list>
         </property>.

类路径:config/export/export.properties
.

在这个spring之后,可以加载属性,但在进一步的处理中,我在logger中有以下代码:
DOMConfigurator.configure(“config/export/export\u log.xml”)
,它失败于
“java.io.FileNotFoundException:H:\workspace\export\job\config/export/export\u log.xml”.
它是如何获取此路径的?

我可以通过如下配置日志文件来解决此问题:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:context.xml"})
public class Test1 {

@Autowired
Export export;
    @org.junit.Test
    public void test2() {
        export.report();
DOMConfigurator.configure(JobLogger.class.getClassLoader().getResource("config/export/export_log.xml"))

我可以通过如下配置日志文件来解决此问题:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:context.xml"})
public class Test1 {

@Autowired
Export export;
    @org.junit.Test
    public void test2() {
        export.report();
DOMConfigurator.configure(JobLogger.class.getClassLoader().getResource("config/export/export_log.xml"))

使用
classpath:
而不是
file:
。我很惊讶它使用
java-jar
工作。@M.Deinum提出的解决方案是正确的。@M.Deinum由于我的属性是外部化的,所以我在代码中使用了file:。但是我尝试创建了一个test-context.xml并将它放在src/test/resources中。虽然现在它可以找到config\export\export.properties,但在进一步处理后,我在logger:DOMConfigurator.configure中有以下代码(“config/export/export_log.xml”);此操作失败,出现“java.io.FileNotFoundException:H:\workspaces\export\job\config/export/export_log.xml”。它为什么要查看项目的绝对路径?它是如何选择此路径的?我无法格式化我的评论。已在问题中添加了详细信息。不,它不是外部化的,它位于类路径上…因此您需要使用
类路径:
文件:
前缀用于不在类路径上的文件。使用
类路径:
而不是
文件:
。我很惊讶它使用
java-jar
工作。由@m.Deinum提出的解决方案是正确的。@m.Deinum由于我的属性被外部化,我在代码中使用了file:。但是我尝试创建了一个test-context.xml并将它放在src/test/resources中。尽管现在它能够找到config\export\export.properties但在进一步的处理过程中,我在logger中有以下代码:DOMConfigurator.configure(“config/export/export_log.xml”);这在“java.io.FileNotFoundException:H:\workspace\export\job\config/export/export_log.xml”中失败。它为什么要查看项目的绝对路径?它是如何选择此路径的?我无法格式化我的评论。已在问题中添加了详细信息。不,它不是外部化的,它位于类路径上…因此您需要使用
类路径:
文件:
前缀用于不在类路径上的文件。使用
类路径:
而不是
文件:
。我很惊讶它使用
java-jar
工作。由@m.Deinum提出的解决方案是正确的。@m.Deinum由于我的属性被外部化,我在代码中使用了file:。但是我尝试创建了一个test-context.xml并将它放在src/test/resources中。尽管现在它能够找到config\export\export.properties但在进一步的处理过程中,我在logger中有以下代码:DOMConfigurator.configure(“config/export/export_log.xml”);这在“java.io.FileNotFoundException:H:\workspace\export\job\config/export/export_log.xml”中失败。它为什么要查看项目的绝对路径?它是如何选择此路径的?我无法格式化我的评论。已在问题中添加了详细信息。不,它不是外部化的,它位于类路径上…因此您需要使用
类路径:
文件:
前缀用于不在类路径上的文件。