java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3882字,纯文字阅读大概需要6分钟。
内容图文
![java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件](/upload/InfoBanner/zyjiaocheng/670/4ddc35b1c8ad4984b4c826269115e967.jpg)
我在通过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进程.
内容总结
以上是互联网集市为您收集整理的java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件全部内容,希望文章能够帮你解决java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。