Ant-TestNG classpath issue: getResource can’t find the file

I got a TestNG test, that works perfectly fine on it’s own. Somewhere in it I got:

URL fileName = this.getClass().getClassLoader().getResource("config.properties");

I’m trying to run the same test using Ant:

<testng outputDir="${resultsFolder}" haltOnFailure="false">
  <classpath>
      <fileset dir="./src/">
        <include name="**/*.properties"/>
      </fileset>
            -----------------------------...--------------------
 </classpath>
 <classfileset dir="correct path here" includes="**/*.class" />
</testng>

I can see in debug mode that the config.properties is in the classpath. But the line at the top can not find it, it is null.

EDIT: I solved it.The critical line actually does not search for the file in the classpath directly, it searches IN the files/folders. So, this solved my problem:

<pathelement location="src/"/>

Thanks for the help.

Answer

try to replace your <classpath>...</classpath> with this:

<classpath>
     <pathelement path="./src"/>
</classpath>

In order for JVM to find the config.properties the parent directory of config.properties should be in classpath.

Leave a Reply

Your email address will not be published. Required fields are marked *