Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource files not found from JUnit test cases

Summary

My JUnit tests are not finding the files they require during execution. I'm using Maven for dependency management and compilation.

Details

All files required by the test cases are located in: src/test/resources.

For example, 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); 

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); 

It seems as though the files from src/test/resources are not getting copied into target/test-classes.

Any ideas?

The following questions did not help

Why Can't I access src/test/resources in Junit test run with Maven?

Loading Properties File In JUnit @BeforeClass

How to deal with the test data in Junit?

Software Versions

Ubuntu 12.04

Apache Maven 2.2.1

Java 1.7.0

Eclipse (Java EE IDE for Web Developers) Indigo Service Release 2

(truncated) 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> 
like image 713
Alex Averbuch Avatar asked May 10 '12 14:05

Alex Averbuch


2 Answers

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")‌​); 

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(); 
like image 99
Alex Averbuch Avatar answered Sep 19 '22 16:09

Alex Averbuch


You know that Maven is based on the Convention over Configuration pardigm? so you shouldn't configure things which are the defaults.

All that stuff represents the default in Maven. So best practice is don't define it it's already done.

    <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> 
like image 42
khmarbaise Avatar answered Sep 23 '22 16:09

khmarbaise