title: Access Resources in Unit Tests
body: <p>I'm using JUnit 4, Java 8, and Gradle 1.12.
  I have a file with default json that I need to load. My project has <code>src/main/java/</code> (containing the project source), <code>src/main/resources/</code> (empty), <code>src/test/java/</code> (unit test source), and <code>src/test/resources/</code> (the json data file to load) directories. The <code>build.gradle</code> file is in the root.</p>
  
  <p>In my code I have:</p>
  
  <pre><code>public class UnitTests extends JerseyTest
  {
    @Test
    public void test1() throws IOException
    {
      String json = UnitTests.readResource("/testData.json");
      // Do stuff ...
    }
  
    // ...
    private static String readResource(String resource) throws IOException
    {
      // I had these three lines combined, but separated them to find the null.
      ClassLoader c = UnitTests.class.getClassLoader();
      URL r = c.getSystemResource(resource); // This is returning null. ????
      //URL r = c.getResource(resource); // This had the same issue.
      String fileName = r.getFile();
      try (BufferedReader reader = new BufferedReader(new FileReader(fileName)))
      {
        StringBuffer fileData = new StringBuffer();
        char[] buf = new char[1024];
        int readCount = 0;
        while ((readCount = reader.read(buf)) != -1)
        {
          String readData = String.valueOf(buf, 0, readCount);
          fileData.append(readData);
        }
  
        return fileData.toString();
      }
    }
  }
  </code></pre>
  
  <p>From what I read, that should give me access to the resource file. However, I get a null pointer exception when I try to use the URL, because the <code>getSystemResource()</code> call returns null.</p>
  
  <p>How do I access my resource files?</p>