2

I am running into trouble loading a local file that I'd like to use in a JUnit test inside of my Android application.

Here is my application structure:

app
    java
        demoproject
            MainActivity.java
        demoproject(androidTest)
            ExampleInstrumentedTest.java
        demoproject(test)
            ExampleUnitTest.java
            testValues.json
    res

ExampleUnitTest.java

package com.example.zach.demoproject;

import org.junit.Test;

import java.io.File;
import java.net.URL;

public class ExampleUnitTest {
    @Test public void checkValues() throws Exception {
        URL valuesURL = getClass().getResource("testValues.json");
        File valuesFile = new File(valuesURL.getFile());

        // ... test logic with valuesFile.
    }
}

When I run the checkValues test it fails with a java.lang.NullPointerException at the File valuesFile = new File(valuesURL.getFile()); line - the valuesURL object is null.

I am using Android Studio, with a more or less "new" application. The only new code I added was this unit test/testValues file to demonstrate my issues.

Any thoughts?

Zach
  • 3,909
  • 6
  • 25
  • 50
  • 1
    I suggest to take a look at: http://stackoverflow.com/questions/29341744/android-studio-unit-testing-read-data-input-file problem is about test classpath – uylmz Dec 16 '16 at 18:55
  • Thanks for the awesome link. I went through there and tried the answer out and wasn't getting it to work - I found out it was a dumb mistype on my part. – Zach Dec 16 '16 at 19:12

0 Answers0