加载junit的资源文件在eclipse中工作但不在任何命令行上工作?

时间:2013-07-30 15:41:28

标签: java ant junit

我有以下代码,由junit 4测试类调用:

final InputStream resourceAsStream = getClass().getResourceAsStream("MyFile.txt");

这在eclipse中工作,但在使用ant运行时在命令行上失败。

注意,我试过了:

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("MyFile.txt"));

Foo.class.getResourceAsStream( “MyFile.txt的”))

但没有运气。

我的测试类,资源文件和代码都在目录中:

myproject/tests/src/junit4/org/user/util/services/service

我是否必须执行/添加build.xml以使其正常工作?

这是我运行测试的主build.xml的一部分:

<target name="junit4" depends="compile-junit4" description="compile and run the junit4 tests">
        <ant antfile="${build.rootdir}/make/junit4.xml" dir="${build.rootdir}" target="all" inheritAll="true" inheritRefs="true" />
    </target>

这是蚂蚁文件:

<project name="build" default="all" basedir=".">

    <target name="common-init">
        <property name="junit.tmpdir" value="test-tmpdir" />
        <mkdir dir="${junit.tmpdir}" />
        <property name="jvm.args" value="-ea -Xms32m -Xmx384m -server -Djava.io.tmpdir=${junit.tmpdir} -Dcom.sun.grizzly.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true" />
        <!-- runtime classpath of our junit task -->
        <path id="junit4-runtime-classpath">
            <path path="${build.rootdir}/external/junit-4.7.jar" />
        </path>

        <!-- Directory for classes, relative to base directory /-->
        <property name="build.classes.dir" value="${build.rootdir}/build/classes/${build.type}" />

        <mkdir dir="${build.rootdir}/tests/reports" />

        <path id="test.cp">
            <!--    
            uncomment this to use instrumented classes
      <pathelement path="${instrumented.dir}"/>
        -->
            <pathelement path="${build.tool.classes.dir}" />
            <pathelement path="${build.classes.dir}" />

            <!--
            uncomment this to use instrumented classes
      <fileset dir="${build.rootdir}/external">
        <include name="cobertura/*.jar"/>
      </fileset>
      -->

            <path refid="external.jars.path" />
        </path>

        <!-- default log.properties to use for all tests -->
        <property name="test.log.properties" value="${build.rootdir}/tests/src/unit/log.properties" />

        <!-- if set on the command line, pass down the test.propertyFile for global test properties
         that are to be used for all tests.  should be the name of a Java Properties file
         in the test root directory (of each test), or an absolute path that starts with / -->
        <property name="test.propertyFile" value="" />
        <property name="smoketest.propertyFile" value="" />
    </target>

    <target name="junit4-init" depends="common-init">
        <property name="test.type" value="Junit4" />
        <!-- Unit tests java Srcs dir -->
        <property name="unit.java.dir" value="tests/src/junit4" />
    </target>

    <target name="selenium-init" depends="common-init">
        <property name="test.type" value="Selenium" />
        <!-- Unit tests java Srcs dir -->
        <property name="unit.java.dir" value="tests/src/selenium" />
    </target>

    <property name="tmp.dir" value="${build.rootdir}/build/.tmp.ant" />

    <!-- because we are running tests in the test src directory
       we need to clean up the transient files.
       won't be needed if we were running tests properly in a separate area -->
    <target name="clean-testleftovers">
        <echo message="Directory is ${build.rootdir}" />
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${build.rootdir}/tests/src" erroronmissingdir="true">
                <include name="**/systemdb/**/*" />
                <include name="**/prefs" />
                <include name="**/prefs/*" />
                <include name="**/scm/**/*" />
                <include name="**/security/**/*" />
                <include name="**/security" />
                <include name="**/membership/*" />
                <include name="**/sequences" />
                <include name="**/passwd/*" />
                <include name="**/*/*.snapshot" />
                <include name="**/sidelinedSchedulers" />
                <!-- exclude any java files prefs or log properties that we may have in directories called prefs/scm etc. -->
                <exclude name="**/*.java" />
                <exclude name="**/pref*.xml" />
                <exclude name="**/.privileges.xml" />
                <exclude name="**/log.properties" />
            </fileset>
        </delete>
    </target>

    <target name="runbatch" depends="clean-testleftovers">
        <echo message="Running ${test.type} testcases in batch mode .." />
        <mkdir dir="${build.rootdir}/tests/reports" />
        <mkdir dir="${build.rootdir}/tests/reports/logs" />
        <mkdir dir="${coverage.dir}" />

        <!-- clean out old test reports -->
        <!-- <delete>
            <fileset dir="${build.rootdir}/tests/reports/junit4">
                <include name="TEST-*" />
                <include name="TESTS*.xml" />
            </fileset>
            <fileset dir="${build.rootdir}/tests/reports/junit4/logs">
                <include name="*.log" />
            </fileset>
        </delete> -->

        <!-- clean out old coverage reports -->
        <delete>
            <fileset dir="${coverage.dir}">
                <include name="*.xml" />
                <include name="*.html" />
            </fileset>
        </delete>


        <!-- Timeout individual test in 15 minutes -->
        <junit printsummary="withOutAndErr" haltonfailure="no" errorProperty="test.failed" failureProperty="test.failed" timeout="900000">
            <jvmarg line="${jvm.args}" />
            <!--      <sysproperty key="prefs.file.name"     value="${prefs.file.name}"/> -->
            <sysproperty key="com.rascal.inprocess" value="true" />
            <sysproperty key="base.prefs.root" value="${build.rootdir}/${unit.java.dir}" />
            <sysproperty key="build.root.dir" value="${build.rootdir}" />
            <sysproperty key="perl.path" value="${perl.path}" />
            <sysproperty key="java.io.tmpdir" value="${java.io.tmpdir}" />
            <sysproperty key="test.propertyFile" value="${test.propertyFile}" />
            <sysproperty key="smoketest.propertyFile" value="${smoketest.propertyFile}" />
            <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.ser}" />
            <formatter type="xml" />
            <classpath refid="test.cp" />
            <batchtest fork="yes" todir="${build.rootdir}/tests/reports">
                <fileset dir="${build.rootdir}/${unit.java.dir}">
                    <include name="**/*Test*.java" />
                </fileset>
            </batchtest>
        </junit>

        <junitreport todir="${build.rootdir}/tests/reports">
            <fileset dir="${build.rootdir}/tests/reports">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="noframes" todir="${build.rootdir}/tests/reports" />
        </junitreport>

    </target>

    <target name="coverage-report" if="enable.coverage">
        <cobertura-report format="html" datafile="${cobertura.ser}" destdir="${coverage.dir}" srcdir="src/java" />
        <cobertura-report format="xml" datafile="${cobertura.ser}" destdir="${coverage.dir}" srcdir="src/java" />
        <echo>Coverage report is available at ${coverage.dir}/index.html</echo>
    </target>

    <target name="all" depends="junit4-init, runbatch">
        <fail if="test.failed">
        JUnit4 tests failed. Check reports for details
        </fail>
    </target>

    <target name="selenium-tests" depends="selenium-init, runbatch">
        <fail if="test.failed">
        Selenium tests failed. Check reports for details
        </fail>
    </target>

</project>

非常感谢!

1 个答案:

答案 0 :(得分:0)

您应该在“build.xml”中为junit运行时定义类路径。

你可以把你的“build.xml”内容粘贴到那里吗?感谢。