title: getResourceAsStream from JUnit
body: <p>I am creating a JUnit TestCase for a project which needs to load a configuration file during initialization.</p>
  
  <p>This configuration file is inside the project in src/main/resources/config folder and during the build maven places it into /config folder, inside the JAR.</p>
  
  <p>The initialization class, reads the file from there using this statement:</p>
  
  <pre><code>ClassLoader classloader = this.getClass().getClassLoader();
  BufferedReader xmlSource = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream("/config/config.xml")));
  </code></pre>
  
  <p>The problem I have is that when I deploy and execute this jar into the application server it works as expected, however, whenever I run it in a JUnit TestCase within Eclipse, the getResrouceAsStream method returns null.</p>
  
  <p>Considering that the class is my.package.MyClassTest.java, and that it lives in src/test/java/my/package/MyClassTest.java, I already tried placing a copy of the config.xml file into the following folders without success:</p>
  
  <pre><code>- src/test/resources/config
  - src/test/resources/my/package/config
  - src/test/java/my/package/config
  </code></pre>
  
  <p>I know that similar questions have been asked many times here in StackOverflow, but all the responses I found refer to changing the way the file is loaded and, although changing the code may be an option, I would prefer to just find the right place for the file so I do not need to modify things which already work in the production environment.</p>
  
  <p>So, where should I place this file to be able to use it in my JUnit test?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I just came up with the solution with a small change in the code:
  Instead of using the ClassLoader to get the resource, I directly used the class:</p>
  
  <pre><code>Class clazz = this.getClass();
  BufferedReader xmlSource = new BufferedReader(new InputStreamReader(clazz.getResourceAsStream("/config/config.xml")));
  </code></pre>
  
  <p>And it reads the file successfully from src/test/resources/config/config.xml.</p>
  
  <p>However, there's is something very weird here:
  The Class.getResourceAsStream method is:</p>
  
  <pre><code>public InputStream getResourceAsStream(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader0();
      if (cl==null) {
          // A system class.
          return ClassLoader.getSystemResourceAsStream(name);
      }
      return cl.getResourceAsStream(name);
  }
  </code></pre>
  
  <p>And if I debug it, I can clearly see that this <strong>getClassLoader0()</strong> returns exactly the same object (same id) than the previous call, <strong>this.getClass().getResourceAsStream()</strong> (which I maintained, just to compare the values)!!!</p>
  
  <p>What's going on here?!</p>
  
  <p>Why does calling the method directly not work, while inserting a new method call in between works?</p>
  
  <p>Honestly, I'm really astonished in front of this.</p>
  
  <p>BTW, I am using JUnit version 4.10. May it be tampering the getClassLoader call in some way?</p>
  
  <p>Many thanks,</p>
  
  <p>Carles</p>