我在通过Ant运行Junit测试时遇到问题.我似乎无法让Ant看到加载项目所需的dll所需的属性文件.我所有的测试都使用Elcipse中的Junit GUI进行,所以我很确定测试本身不是问题.我认为我的问题与类路径有关,但是我似乎找不到问题.
罐子结构:
/root/folder/../Foo.properties
这是在库中加载属性文件的方式:
// load class properties
props = PropertyLoader.loadProperties(Foo.class);
public static Properties loadProperties(Class className) {
return loadProperties(className.getName());
}
public static Properties loadProperties(final String propsName) {
Properties props = null;
InputStream in = null;
try {
ClassLoader cl = ClassLoader.getSystemClassLoader();
String name = propsName.replace('.', '/').concat(".properties");
in = cl.getResourceAsStream(name);
if (in != null) {
props = new Properties();
props.load(in);
}
}
catch (Exception e) {
props = null;
}
finally {
if (props == null) {
System.err.print("Property file " + propsName + " doesn't exist. System terminated.");
System.exit(0);
}
}
return props;
}
除了有问题的构建文件之外:
<!-- Pattern of source files to copy into classpath-->
<property name="source.files.tocopy"
value="**/*.properties,**/*.dtd,**/*.xml,**/*.jpg" />
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</path>
<!-- Generate Class-Path entry for the JAR's Manifest -->
<pathconvert property="manifest.classpath"
pathsep=" " dirsep="\">
<map from="${basedir}/" to="" />
<fileset dir="lib">
<include name="*.jar" />
</fileset>
</pathconvert>
<!-- Run tests against the JAR -->
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<pathelement location="${target.jar}" />
</path>
<path id="test.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${test.classes.dir}" />
</path>
<!-- - - - - - - - - - - - - - - - - -
target: test-compile
- - - - - - - - - - - - - - - - - -->
<target name="test-compile" depends="compile, test-init"
description="Compiles our testing code">
<javac destdir="${test.classes.dir}"
debug="true"
includeAntRuntime="true"
srcdir="test">
<classpath refid="test.compile.classpath" />
</javac>
<copy todir="${test.classes.dir}">
<fileset dir="test" includes="${source.files.tocopy}"/>
<fileset dir="resources" includes="${source.files.tocopy}"/>
</copy>
</target>
<!-- =================================
target: test
================================= -->
<target name="test" depends="test-compile, optional-tests">
<description>
Runs our tests, generates reports, and stops
the build on failure. Optionally runs one test.
</description>
<junit printsummary="false"
errorProperty="test.failed"
failureProperty="test.failed">
<classpath>
<path refid="test.classpath" />
</classpath>
<sysproperty key="test.properties" value="${test.properties.file}"/>
<formatter type="brief" usefile="false" />
<formatter type="xml" />
<test name="${testcase}" todir="${test.data.dir}" if="testcase" />
<batchtest todir="${test.data.dir}" unless="testcase">
<fileset dir="${test.classes.dir}">
<patternset>
<include name="**/test/*Test.class" />
<exclude name="**/test/*Printer*.class" unless="test.properties.file" />
</patternset>
</fileset>
</batchtest>
</junit>
我真的可以用另一双眼睛,所以任何帮助都值得赞赏.提前致谢!
-查理
解决方法:
尝试使用getResource查看在eclipse中返回的URL,还查看是否可以获取在单元测试中显示的URL.也许您需要传递实际类文件的名称才能获得该文件
clazz.getResource(clazz.getName()+".class")
在运行单元测试之前,请检查文件是否已复制,以便将文件实际放置在运行单元测试时在类路径上使用的输出文件夹中.
当eclipse编译Java文件时,它还将所有非Java文件自动复制到输出目录.这就是为什么可以在Eclipse的类路径中访问文件的原因.如果在编译过程中未将非Java文件复制到构建目标文件夹,则不会复制属性文件.尽管您已经准备好复制文件,但它确实会从您的构建文件中查找.浏览文件夹时可以验证相同吗?
另外,除了小型程序外,永远不要在代码中使用System.exit(0).最好具有传播到主函数并终止程序的异常.原因是您最终获得了一个库,该库将在使用Java进程失败时终止Java进程.
标签:eclipse,java,ant 来源: https://codeday.me/bug/20191108/2004689.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。