1

In a project I want to use some sample JSON files from src/test/resources in my test classes. The project can be found on https://github.com/kamikat/moshi-jsonapi if you want to test it for yourself. The project setup looks like this: project setup

And in my tests I load the resource files like this:

public static String readResource(String resourceName) {
    Scanner scanner = new Scanner(TestResources.class.getResourceAsStream(resourceName), "UTF-8");
    return scanner.useDelimiter("\\A").next();
}

public static String getPrivateSample() {
    return readResource("/private.json");
}

So far, so good. To add the *.json files to my classpath, I needed to add this to the build.gradle file:

sourceSets {
    test {
        resources {
            srcDir "resources"
            includes["**/*.json"]
        }
    }
}

Now I can run ./gradlew test, and all tests are green - great! But: If I run the tests from IntelliJ (Android Studio 2.2 RC1), the following happens:

java.lang.NullPointerException: source

    at java.util.Objects.requireNonNull(Objects.java:228)
    at java.util.Scanner.<init>(Scanner.java:578)
    at moe.banana.jsonapi2.TestResources.readResource(TestResources.java:8)
    at moe.banana.jsonapi2.TestResources.getPrivateSampleSerialized(TestResources.java:17)
    at moe.banana.jsonapi2.PrivateAndProtectedTest.serializePrivate(PrivateAndProtectedTest.java:58)
    at [...]

The problem appears to be that the JSON files are not added to the classpath when the tests run from IntelliJ. I can fix this temporarily by adding the resources as a manual dependency in the project settings, but I am looking for an automated solution.

I already tried this, this and this, but nothing works. Some answers suggest it's a bug in IntelliJ, which is marked as 'fixed' - but still there is no suitable solution working for me.

Did anyone get his test resources working correctly?

Community
  • 1
  • 1
mreichelt
  • 12,359
  • 6
  • 56
  • 70
  • PS: There is an issue in the open source library I mentioned that is depending on this question: https://github.com/kamikat/moshi-jsonapi/issues/18 – mreichelt Sep 08 '16 at 08:28

2 Answers2

0

You can just make IDEA run the tests using gradle:

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner -> Run tests using: Gradle Test Runner

eekboom
  • 5,551
  • 1
  • 30
  • 39
  • Thanks for your reply! I can't find the 'Build Tools' or the 'Runner' section when I search for them in the Settings. I'm using Android Studio 2.2 RC1, which might be the reason. Still, this would not solve it for me because I'm looking for an automated (gradle-based) solution that works for every new developer automatically. – mreichelt Sep 09 '16 at 10:44
0

This certainly has something to do with your .idea folder. Try to backup your project's current .idea, remove it from the project root and open it in IDEA again so a fresh settings folder will be generated. Then start migrating your settings from your backup folder to the new one (at least the meaningful ones).

I just fixed this issue in my environment with this approach (running Java test but test resources were not being recognized).