我正在使用Ant 1.8,JUnit 4.8.2。我试图加载一个属性文件,并有一些问题。属性文件位于我的源代码目录的根目录,并将其加载到类路径中,以及在类路径中显式加载属性文件。下面是我的ant build.xml文件。这是我如何加载属性...如何使用JUnit/Ant加载属性文件?
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
它总是失败与空(属性未加载)。
<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>
任何人都知道加载属性文件的正确方法?谢谢, - 戴夫
这听起来很对我。我认为访问ContextClassLoader的首选方法是通过当前类。所以(我认为)这会更好:getClass()。getContextClassLoader()。getResourceAsStream(“db.properties”); – 2011-05-06 20:31:37
获胜者!格维兹,你为什么说一种方式比另一种更受欢迎? – Dave 2011-05-06 20:37:43