【问题标题】:Unable to Locate Properties File Inside Jar During Junit Tests via Ant在通过 Ant 进行 Junit 测试期间无法在 Jar 中找到属性文件
【发布时间】:2008-12-08 00:37:31
【问题描述】:

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

jar 结构: /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>

我真的可以使用第二双眼睛,所以任何帮助都非常感谢。提前致谢!

--查理

【问题讨论】:

    标签: java eclipse ant


    【解决方案1】:

    尝试使用 getResource 来查看 eclipse 中返回的 URL,并查看是否可以获取 url 以显示在单元测试中。也许您需要传递实际类文件的名称来获取它

    clazz.getResource(clazz.getName()+".class")
    

    在运行单元测试之前检查文件是否已复制,以便文件实际放置在运行单元测试时在类路径上使用的输出文件夹中。

    eclipse在编译java文件时,也会自动将所有非java文件复制到输出目录。这就是为什么在 Eclipse 的类路径中可以访问该文件的原因。如果在编译期间不将非 java 文件复制到构建目标文件夹,则不会复制属性文件。尽管您已经准备好复制文件,但它确实从您的构建文件中查看。浏览文件夹时可以验证相同吗?

    此外,您不应该在代码中使用 System.exit(0),小程序除外。最好让异常传播到主函数并终止程序。原因是您最终会得到一个库,该库会在 Java 进程被使用和失败时终止。

    【讨论】:

      【解决方案2】:

      我发现在某些情况下,jar 中的属性文件不能位于根文件夹中。将其移动到 jar 内的目录中,然后使用 getResourceAsStream。

      卡尔

      【讨论】:

        【解决方案3】:

        ant调试的第一步总是:

        1. ant -verbose
        2. ant -debug

        查看与junit 测试相关的目标的输出。

        如果您在那里找到解决问题的其他信息,那太好了!如果没有,请使用输出中的相关信息编辑问题。

        【讨论】:

          猜你喜欢
          • 2014-08-03
          • 2012-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-23
          • 1970-01-01
          • 2017-08-30
          • 1970-01-01
          相关资源
          最近更新 更多