为什么我不能在使用Maven的Junit测试运行中访问src / test / resources? [英] Why Can't I access src/test/resources in Junit test run with Maven?

查看:830
本文介绍了为什么我不能在使用Maven的Junit测试运行中访问src / test / resources?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行以下代码时遇到问题:

  configService.setMainConfig(src / test / resources / MainConfig。 XML); 

来自Junit @Before方法。



<这是Maven构建其目标文件夹的方式吗?

解决方案

访问 MainConfig.xml 直接。 src / test / resources 目录内容放在CLASSPATH的根目录中。



更确切地说:内容 src / test / resources 被复制到 target / test-classes 中,所以如果你有以下项目结构: / p>

 。 $ b $b└──src$ b $b└──test$ b $b├──jP$ b $b│└──foo$ b $b│└──Cjava$ b $b└── ─资源$ b $b├──a.xml$ b $b└──foo$ b $b└──b.xml

这将导致以下测试CLASSPATH内容:




  • / foo / C.class

  • /a.xml

  • /foo/b.xml



要从Java源实际访问文件,使用
getClass()。getResource(/ MainConfig.xml)。getFile()


I am having a problems running the following code:

configService.setMainConfig("src/test/resources/MainConfig.xml");

From within a Junit @Before method.

Is this the way Maven builds out its target folder?

解决方案

Access MainConfig.xml directly. The src/test/resources directory contents are placed in the root of your CLASSPATH.

More precisely: contents of src/test/resources are copied into target/test-classes, so if you have the following project structure:

.
└── src
    └── test
        ├── java
        │   └── foo
        │       └── C.java
        └── resources
            ├── a.xml
            └── foo
                └── b.xml

It will result with the following test CLASSPATH contents:

  • /foo/C.class
  • /a.xml
  • /foo/b.xml

To actually access the files from Java source, use getClass().getResource("/MainConfig.xml").getFile().

这篇关于为什么我不能在使用Maven的Junit测试运行中访问src / test / resources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆