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?