nibuen
nibuen

Reputation: 763

Android Studio with Java Library Module Unable to load test resources

I have an Android Studio Project with a Java library inner module that has tests with test resources. Yet when i run the tests I am not able to retrieve the artifacts. This seems to work fine in a pure java gradle project (in eclipse at least).

meaning For java plugin:

src/main/java
src/main/test
src/test/java
src/test/resources

Under the resources directory i have a crt file that i want to load in my junit test. When using any command i have come across it returns null for the resource. Yet I have confirmed the resources are in the build folder.

Some things I Tried:

getClass().getClassLoader().getResourceAsStream("cert_format_der.crt").read(); // NPE
getClass().getClassLoader().getResourceAsStream("/cert_format_der.crt").read(); // NPE
getClass().getClassLoader().getSystemResourceAsStream("/cert_format_der.crt").read(); // NPE

Thanks

Upvotes: 10

Views: 1253

Answers (2)

Tomasito665
Tomasito665

Reputation: 1209

Instead of diverging the resources to the class output directory − as suggested by nibbuen − it is also possible not to touch the output directory and explicitly add it as a dependency.

dependencies {
  runtime files(sourceSets.test.output.resourcesDir)
}

Upvotes: 5

nibuen
nibuen

Reputation: 763

Turns out this seems to be a bug with Intellij and how Gradle not setting the resource directory for the test sourcesets correctly.

Adding This to the build.gradle for the module Fixes it:

sourceSets {
   test {
      output.resourcesDir = output.classesDir
   }
}

Upvotes: 18

Related Questions