# How do I include java files as test resources?

**URL:** <https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221>\
**Category:** Old Forum Archive\
**Created:** [November 30, 2013, 1:30pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221 "2013-11-30T13:30:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Nix](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@Chris\_Nix](https://discuss.gradle.org/u/Chris_Nix)\
**Post date:** [November 30, 2013, 1:30pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221/1 "2013-11-30T13:30:00Z")

</div>

I am writing a Java annotation processor. My tests perform a compilation step, with test resources that include Java files. These tests complete fine if I statically reference the Java resource files by their path and filenames. However, I was hoping to protect against name refactorings of those resources files by grabbing the file URL as follows:

```gradle
ClassLoader.getSystemResource(JavaClassIWantToTest.class.getSimpleName())

```

My IDE (IntelliJ Idea) is fine with this, however…

The gradle ‘resources’ source set appears to exclude java files by default, as described at [http://www.gradle.org/docs/current/userguide/java\_plugin.html](http://www.gradle.org/docs/current/userguide/java_plugin.html). Subsequently, my ‘compileTestJava’ task fails, unable to find the referenced files for compilation.

I’ve tried:

```gradle
sourceSets {
    test {
        resources {
            include '**/*.java'
        }
    }
}

```

to no avail.

Indeed,

```gradle
sourceSets {
    test {
        resources.each { File f -> println f }
    }
}

```

shows that the java files are included at this stage anyway, which might explain why my ‘include’ has no effect.

So how can I include java files as test resources for my annotation processor?

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 30, 2013, 2:27pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221/2 "2013-11-30T14:27:00Z")

</div>

I don’t think Java files get filtered from resources. And doesn’t the problem occur when _running_ the test, rather than when compiling it? Anyway, it’s probably a matter of figuring out the right code to load the resources. If you need a ‘File’, try something like this (not tested):

```gradle
String resourceName = JavaClassIWantToTest.class.getName().replace(".", "/");
URL resource = getClass().getClassLoader().getResource(resourceName);
File file = new File(resource.toURI());

```

Since you are referring to the class from the code (‘JavaClassIWantToTest.class’), you need to have the class under ‘src/test/java’ (or your test sources directory of choice). As a consequence, it will already be on the (test runtime) class path, and you don’t also have to make it a (Gradle) resource.

If an ‘InputStream’ is sufficient, ‘getClass().getClassLoader().getResourceAsStream(resourceName)’ is preferable.

---

<div class="post-metadata">

**Author:** ![Chris\_Nix](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@Chris\_Nix](https://discuss.gradle.org/u/Chris_Nix)\
**Post date:** [November 30, 2013, 6:51pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221/3 "2013-11-30T18:51:00Z")

</div>

Thanks for taking time to consider this.

The test fails to compile.

I tried your suggestion, but unfortunately I get the same error, indicating to me that the Java file in /src/test/java is not on the classpath for compilation.

```gradle
error: cannot find symbol
 String resourceName = JavaClassIWantToTest.class.getName().replace(".", "/");
                       ^

```

If I statically code the String ‘resouceName’ as “com/example/package/name/JavaClassIWantToTest.java”, then the test works.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 30, 2013, 7:10pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221/4 "2013-11-30T19:10:00Z")

</div>

The sources to be compiled are never on their own compile class path, but of course they can still refer to each other. Perhaps you didn’t fully explain your setup.

---

<div class="post-metadata">

**Author:** ![Chris\_Nix](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@Chris\_Nix](https://discuss.gradle.org/u/Chris_Nix)\
**Post date:** [November 30, 2013, 7:11pm UTC](https://discuss.gradle.org/t/how-do-i-include-java-files-as-test-resources/6221/5 "2013-11-30T19:11:00Z")

</div>

Thanks for taking time to consider this.

The test fails to compile.

I tried your suggestion, but unfortunately I get the same error, indicating to me that the Java file in /src/test/java is not on the classpath for compilation.

```gradle
error: cannot find symbol
 String resourceName = JavaClassIWantToTest.class.getName().replace(".", "/");
                       ^

```

If I statically code the String ‘resouceName’ as “com/example/package/name/JavaClassIWantToTest.java”, then the test works.
