打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java-在通过Ant进行Junit测试期间无法在Jar中找到属性文件

我在通过Ant运行Junit测试时遇到问题.我似乎无法让Ant看到加载项目所需的dll所需的属性文件.我所有的测试都使用Elcipse中的Junit GUI进行,所以我很确定测试本身不是问题.我认为我的问题与类路径有关,但是我似乎找不到问题.

罐子结构:
/root/folder/../Foo.properties

这是在库中加载属性文件的方式:

// load class propertiesprops = 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进程.

来源:https://www.icode9.com/content-1-551151.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
无所不能的“蚂蚁”--Ant(一)(二)(三)(四)
用Ant自动测试JUnit(Ant测试自动化--上)
怎样用Ant测试Spring项目(Ant测试自动化--下
Ant脚本文件build文件编写示例
Apache Ant使用进阶
build-example.xml(ant+junit测试报告)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服