title: Loading Properties File In JUnit @BeforeClass
body: <p>I'm trying to load sample.properties from my classpath during my JUnit test execution and it can't find the file in the class path.  If I write a Java Main class I'm able to load the file just fine. I'm using the below ant task to execute my JUnit. </p>
  
  <pre><code>public class Testing {
   @BeforeClass
      public static void setUpBeforeClass() throws Exception {   
          Properties props = new Properties();
          InputStream fileIn = props_.getClass().getResourceAsStream("/sample.properties");
          **props.load(fileIn);**
      }
  
  }
  </code></pre>
  
  <p><strong>JUnit:</strong></p>
  
  <pre><code>&lt;path id="compile.classpath"&gt;
          &lt;pathelement location="${build.classes.dir}"/&gt;
      &lt;/path&gt;
      &lt;target name="test" depends="compile"&gt;
              &lt;junit haltonfailure="true"&gt;
                  &lt;classpath refid="compile.classpath"/&gt;
                  &lt;formatter type="plain" usefile="false"/&gt;
                  &lt;test name="${test.suite}"/&gt;
              &lt;/junit&gt;
          &lt;/target&gt;
          &lt;target name="compile"&gt;
              &lt;javac srcdir="${src.dir}" 
                     includeantruntime="false"
                     destdir="${build.classes.dir}" debug="true" debuglevel="lines,vars,source"&gt;
                  &lt;classpath refid="compile.classpath"/&gt;
              &lt;/javac&gt;
              &lt;copy todir="${build.classes.dir}"&gt;
                  &lt;fileset dir="${src.dir}/resources"
                           includes="**/*.sql,**/*.properties" /&gt;
              &lt;/copy&gt;
          &lt;/target&gt;
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>[junit] Tests run: 0, Failures: 0, Errors: 1, Time elapsed: 0.104 sec
  [junit] 
  [junit] Testcase: com.example.tests.Testing took 0 sec
  [junit]     Caused an ERROR
  [junit] null
  [junit] java.lang.NullPointerException
  [junit]     at java.util.Properties$LineReader.readLine(Properties.java:418)
  [junit]     at java.util.Properties.load0(Properties.java:337)
  [junit]     at java.util.Properties.load(Properties.java:325)
  [junit]     at com.example.tests.Testing.setUpBeforeClass(Testing.java:48)
  [junit]
  </code></pre>