从JUnit测试用例中找不到资源文件 [英] Resource files not found from JUnit test cases
问题描述
摘要
我的JUnit测试未找到执行期间所需的文件。
我正在使用Maven进行依赖关系管理和编译。
My JUnit tests are not finding the files they require during execution. I'm using Maven for dependency management and compilation.
详细信息
测试用例所需的所有文件都位于: src / test / resources
。
All files required by the test cases are located in: src/test/resources
.
例如, src / test / resources / resourceFile.txt
。
要访问资源,我使用以下代码:
To access a resource I use the following code:
URL url = getClass().getResource("/resourceFile.txt").getFile();
File file = new File(url);
但是 file.exists()
返回假
。我得到的错误是:
But then file.exists()
returns false
. And the error I get is:
Tests in error:
myJUnitTestCase(tests.MyJUnitTestClass): /home/me/workspace/Project%20Name/target/test-classes/resourceFile.txt (No such file or directory)
注意,以下内容给出了相同的错误(请注意已删除的 /
前缀):
Note, the following gives the same error (notice the removed /
prefix):
URL url = getClass().getClassLoader().getResource("resourceFile.txt").getFile();
File file = new File(url);
好像来自 src / test / resources $的文件c $ c>没有被复制到
target / test-classes
。
It seems as though the files from src/test/resources
are not getting copied into target/test-classes
.
有什么想法吗?
以下问题没有帮助
为什么我不能在使用Maven的Junit测试运行中访问src / test / resources ?
软件版本
Ubuntu 12.04
Ubuntu 12.04
Apache Maven 2.2.1
Apache Maven 2.2.1
Java 1.7.0
Java 1.7.0
Eclipse(Web开发的Java EE IDE opers)Indigo Service Release 2
Eclipse (Java EE IDE for Web Developers) Indigo Service Release 2
(截断)Maven POM
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.groupId</groupId>
<artifactId>artifactId</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>name</name>
<build>
<finalName>name</finalName>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
推荐答案
我的错误,资源文件WERE实际上已复制到目标/测试类
。 问题似乎是由于我的项目名称中的空格,例如项目%20Name
。
My mistake, the resource files WERE actually copied to target/test-classes
. The problem seemed to be due to spaces in my project name, e.g. Project%20Name
.
我现在正在加载文件,如下所示:
I'm now loading the file as follows and it works:
org.apache.commons.io.FileUtils.toFile(myClass().getResource("resourceFile.txt"));
或者,(取自 Java:如何从转义的URL获取文件?)这可能更好(不依赖于Apache Commons) :
Or, (taken from Java: how to get a File from an escaped URL?) this may be better (no dependency on Apache Commons):
myClass().getResource("resourceFile.txt").toURI();
这篇关于从JUnit测试用例中找不到资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!