如何使用 JUnit / Ant 加载属性文件?

发布于 2024-11-05 06:37:02 字数 2209 浏览 0 评论 0原文

我正在使用 Ant 1.8、JUnit 4.8.2。我正在尝试加载属性文件并遇到一些问题。属性文件位于源目录的根目录中,我将其加载到类路径中,并在类路径中显式加载属性文件。下面是我的 ant build.xml 文件。这是我尝试加载属性的方法......

private void loadTestProperties() { 
    try { 
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream("db.properties");
        prop.load(in);
        in.close();
    } catch (Exception e) { 
        fail("Failed to load properties: " + e.getMessage());
    }   // try
}   // loadTestProperties

它总是失败并返回 null(属性未加载)。

<project name="leads-testing" default="build" basedir=".">
  <property name="tst-dir" location="/Users/davea/Documents/workspace-sts-2.6.0.SR1/infinitiusa_leads_testing/test" />
  <property name="db-props-file" location="/Users/davea/Documents/workspace-sts-2.6.0.SR1/infinitiusa_leads_testing/test/db.properties" />
  <property name="TALK" value="true" />

  <path id="classpath.base">
  </path>
  <path id="classpath.test">
    <pathelement location="lib/junit-4.8.2.jar" />
    <pathelement location="lib/selenium-java-client-driver.jar" />
    <pathelement location="lib/classes12.jar" />
    <pathelement location="${tst-dir}" />
    <pathelement location="${db-props-file}" />
    <path refid="classpath.base" />
  </path>

  <target name="compile-test">
    <javac srcdir="${tst-dir}"
           verbose="${TALK}"
           >
      <classpath refid="classpath.test"/>
    </javac>
  </target>
  <target name="clean-compile-test">
    <delete verbose="${TALK}">
      <fileset dir="${tst-dir}" includes="**/*.class" />
    </delete>
  </target>

  <target name="test" depends="compile-test">
    <junit>
      <classpath refid="classpath.test" />
      <formatter type="brief" usefile="false" />
      <test name="com.criticalmass.infinitiusa.tests.InfinitiConfigOldG25Base" />
    </junit>
  </target>

  <target name="all" depends="test" />
  <target name="clean" depends="clean-compile-test" />
</project>

有人知道加载属性文件的正确方法吗?谢谢,-戴夫

I'm using Ant 1.8, JUnit 4.8.2. I'm trying to load a properties file and having some problems. The properties file is at the root of my source directory and I load that into the classpath as well as explicitly loading the properties file in the classpath. Below is my ant build.xml file. Here is how I'm trying to load the properties …

private void loadTestProperties() { 
    try { 
        Properties prop = new Properties();
        InputStream in = getClass().getResourceAsStream("db.properties");
        prop.load(in);
        in.close();
    } catch (Exception e) { 
        fail("Failed to load properties: " + e.getMessage());
    }   // try
}   // loadTestProperties

It always fails with null (properties were not loaded).

<project name="leads-testing" default="build" basedir=".">
  <property name="tst-dir" location="/Users/davea/Documents/workspace-sts-2.6.0.SR1/infinitiusa_leads_testing/test" />
  <property name="db-props-file" location="/Users/davea/Documents/workspace-sts-2.6.0.SR1/infinitiusa_leads_testing/test/db.properties" />
  <property name="TALK" value="true" />

  <path id="classpath.base">
  </path>
  <path id="classpath.test">
    <pathelement location="lib/junit-4.8.2.jar" />
    <pathelement location="lib/selenium-java-client-driver.jar" />
    <pathelement location="lib/classes12.jar" />
    <pathelement location="${tst-dir}" />
    <pathelement location="${db-props-file}" />
    <path refid="classpath.base" />
  </path>

  <target name="compile-test">
    <javac srcdir="${tst-dir}"
           verbose="${TALK}"
           >
      <classpath refid="classpath.test"/>
    </javac>
  </target>
  <target name="clean-compile-test">
    <delete verbose="${TALK}">
      <fileset dir="${tst-dir}" includes="**/*.class" />
    </delete>
  </target>

  <target name="test" depends="compile-test">
    <junit>
      <classpath refid="classpath.test" />
      <formatter type="brief" usefile="false" />
      <test name="com.criticalmass.infinitiusa.tests.InfinitiConfigOldG25Base" />
    </junit>
  </target>

  <target name="all" depends="test" />
  <target name="clean" depends="clean-compile-test" />
</project>

Anyone know the correct way to load the properties file? Thanks, - Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-11-12 06:37:02

尝试从 getClass().getResourceAsStream() 加载资源将导致根据类的包名称(即在目录(在类路径中),例如 com/riticmass/infinitiusa/... 。

相反,要从类路径的根加载,请尝试类似的操作

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");

Attempting to load a resource from getClass().getResourceAsStream() will cause db.properties to be looked for based on the package name of the class, i.e. in a directory (in the classpath) like com/criticalmass/infinitiusa/....

Instead, to load from the root of the classpath, try something like

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
隔岸观火 2024-11-12 06:37:02
InputStream in = getClass().getResourceAsStream("db.properties");

请尝试使用 "/db.properties"。请参阅 Class.getResourceAsStream()

对于 Ant,文件路径是相对于工作目录解析的。因此,如果从项目根运行,该文件将位于 src/${db-props-file} 中。

InputStream in = getClass().getResourceAsStream("db.properties");

Try "/db.properties" instead. See Class.getResourceAsStream()

For Ant, file paths are resolved relative to the working directory. So if running from the project root, the file would be in src/${db-props-file}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文