title: Resource files not found from JUnit test cases
body: <p><strong>Summary</strong></p>
  
  <p>My JUnit tests are not finding the files they require during execution.
  I'm using Maven for dependency management and compilation.</p>
  
  <p><strong>Details</strong></p>
  
  <p>All files required by the test cases are located in: <code>src/test/resources</code>.</p>
  
  <p>For example, <code>src/test/resources/resourceFile.txt</code>.</p>
  
  <p>To access a resource I use the following code:</p>
  
  <pre><code>URL url = getClass().getResource("/resourceFile.txt").getFile();
  File file = new File(url);
  </code></pre>
  
  <p>But then <code>file.exists()</code> returns <code>false</code>. And the error I get is:</p>
  
  <pre><code>Tests in error: 
    myJUnitTestCase(tests.MyJUnitTestClass): /home/me/workspace/Project%20Name/target/test-classes/resourceFile.txt (No such file or directory)
  </code></pre>
  
  <p>Note, the following gives the same error (notice the removed <code>/</code> prefix):</p>
  
  <pre><code>URL url = getClass().getClassLoader().getResource("resourceFile.txt").getFile();
  File file = new File(url);
  </code></pre>
  
  <p>It seems as though the files from <code>src/test/resources</code> are not getting copied into <code>target/test-classes</code>.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>The following questions did not help</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/7613359/why-cant-i-access-src-test-resources-in-junit-test-run-with-maven">Why Can&#39;t I access src/test/resources in Junit test run with Maven?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9983426/loading-properties-file-in-junit-beforeclass">Loading Properties File In JUnit @BeforeClass</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2948861/how-to-deal-with-the-test-data-in-junit">How to deal with the test data in Junit?</a></p>
  
  <p><strong>Software Versions</strong></p>
  
  <p>Ubuntu 12.04 </p>
  
  <p>Apache Maven 2.2.1</p>
  
  <p>Java 1.7.0</p>
  
  <p>Eclipse (Java EE IDE for Web Developers) Indigo Service Release 2</p>
  
  <p><strong>(truncated) Maven POM</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.groupId&lt;/groupId&gt;
      &lt;artifactId&gt;artifactId&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;name&lt;/name&gt;
      &lt;build&gt;
          &lt;finalName&gt;name&lt;/finalName&gt;
          &lt;directory&gt;target&lt;/directory&gt;
          &lt;outputDirectory&gt;target/classes&lt;/outputDirectory&gt;
          &lt;testOutputDirectory&gt;target/test-classes&lt;/testOutputDirectory&gt;
          &lt;sourceDirectory&gt;src/main/java&lt;/sourceDirectory&gt;
          &lt;testSourceDirectory&gt;src/test/java&lt;/testSourceDirectory&gt;
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;directory&gt;src/main/resources&lt;/directory&gt;
              &lt;/resource&gt;
          &lt;/resources&gt;
          &lt;testResources&gt;
              &lt;testResource&gt;
                  &lt;directory&gt;src/test/resources&lt;/directory&gt;
              &lt;/testResource&gt;
          &lt;/testResources&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                      &lt;encoding&gt;UTF-8&lt;/encoding&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.10&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>